我在从GUI部分向数据库表插入新记录时遇到问题。我创建了具有id,name,age等的数据库表Patient .... id是identity primary key。我的问题是当我在表中插入重复的名称控件应该转到else部分,并显示消息,如...这个名称已经退出,请尝试使用其他名称... 但在我的编码没有得到.....这里是所有的代码...请有人指出我什么是错的或者这是怎么回事? 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;
}
}
}
这是我的存储过程: CREATE PROCEDURE LoadData ( @Name varchar(50), @Age int, @Gender char(10), @City char(10), @TypeofDisease varchar(50) ) 如 插入患者(姓名,年龄,性别,城市,TypeOfDisease)值(@ Name,@ Age,@ Gender,@ City,@ TypeofDisease) GO
答案 0 :(得分:0)
你确定你的LoadData存储过程正在进行插入而不是更新吗?
答案 1 :(得分:0)
看起来像是在抛出错误,所以它会跳到你的catch块。
您需要处理从PB.ADDPatient返回的错误,而不是值
答案 2 :(得分:0)
记录是否在数据库中重复?我恐怕你没有为数据库中的Names添加唯一约束!
如果是这种情况,并且您使用的是SQL Server,请检查以下内容: SQL Server 2005 How Create a Unique Constraint?