【发布时间】:2011-01-21 22:39:57
【问题描述】:
我在将新记录从 GUI 部分插入数据库表时遇到问题。我创建了带有 id、name、age 等的数据库表 Patient ......id 是身份主键。我的问题是,当我在表中插入重复名称时,控件应该转到其他部分,并显示如下消息...此名称已经存在,请尝试使用另一个名称... 但是在我的编码中没有得到.....这是所有代码...请有人指出我出了什么问题或怎么做??? GUILayer:
protected void BtnSubmit_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
int intResult = 0;
string name = TxtName.Text.Trim();
int age = Convert.ToInt32(TxtAge.Text);
string gender;
if (RadioButtonMale.Checked)
{
gender = RadioButtonMale.Text;
}
else
{
gender = RadioButtonFemale.Text;
}
string city = DropDownListCity.SelectedItem.Value;
string typeofdisease = "";
foreach (ListItem li in CheckBoxListDisease.Items)
{
if (li.Selected)
{
typeofdisease += li.Value;
}
}
typeofdisease = typeofdisease.TrimEnd();
PatientBAL PB = new PatientBAL();
PatientProperty obj = new PatientProperty();
obj.Name = name;
obj.Age = age;
obj.Gender = gender;
obj.City = city;
obj.TypeOFDisease = typeofdisease;
try
{
intResult = PB.ADDPatient(obj);
if (intResult > 0)
{
lblMessage.Text = "New record inserted successfully.";
TxtName.Text = string.Empty;
TxtAge.Text = string.Empty;
RadioButtonMale.Enabled = false;
RadioButtonFemale.Enabled = false;
DropDownListCity.SelectedIndex = 0;
CheckBoxListDisease.SelectedIndex = 0;
}
else
{
lblMessage.Text = "Name [<b>" + TxtName.Text + "</b>] alredy exists, try another name";
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
finally
{
obj = null;
PB = null;
}
}
BAL 层:
public class PatientBAL
{
public int ADDPatient(PatientProperty obj)
{
PatientDAL pdl = new PatientDAL();
try
{
return pdl.InsertData(obj);
}
catch
{
throw;
}
finally
{
pdl=null;
}
}
}
DAL 层:
public class PatientDAL
{
public string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
public int InsertData(PatientProperty obj)
{
SqlConnection con = new SqlConnection(ConString);
con.Open();
SqlCommand com = new SqlCommand("LoadData",con);
com.CommandType = CommandType.StoredProcedure;
try
{
com.Parameters.AddWithValue("@Name", obj.Name);
com.Parameters.AddWithValue("@Age",obj.Age);
com.Parameters.AddWithValue("@Gender",obj.Gender);
com.Parameters.AddWithValue("@City", obj.City);
com.Parameters.AddWithValue("@TypeOfDisease", obj.TypeOFDisease);
return com.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
com.Dispose();
con.Close();
}
}
}
属性类:
public class PatientProperty
{
private string name;
private int age;
private string gender;
private string city;
private string typedisease;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Gender
{
get
{
return gender;
}
set
{
gender = value;
}
}
public string City
{
get
{
return city;
}
set
{
city = value;
}
}
public string TypeOFDisease
{
get
{
return typedisease;
}
set
{
typedisease = value;
}
}
}
这是我的存储过程: 创建过程加载数据 ( @Name varchar(50), @年龄整数, @性别字符(10), @City 字符(10), @TypeofDisease varchar(50) ) 作为 插入患者(姓名、年龄、性别、城市、疾病类型)值(@Name、@Age、@Gender、@City、@TypeofDisease) 去吧
【问题讨论】:
-
您应该重新格式化最后一个清单中的代码。而且您没有提供数据库查询。如我所见,您需要从中返回一个负值才能显示“名称存在”警告。
-
可以分享一下SQL建表SP吗?或者至少是它的结构。