Я столкнулся с проблемой при вставке новой записи из графического интерфейса в таблицу базы данных.Я создал таблицу базы данных Patient с идентификатором, именем, возрастом и т. Д. Идентификатор - это первичный ключ идентификатора.Моя проблема в то время как я вставляю повторяющееся имя в таблицу, элемент управления должен перейти к другой части, и отображать сообщение как ... Это имя уже выходит, пожалуйста, попробуйте с другим именем ... но в моем коде не получается ..... вот весь код ... пожалуйста, кто-нибудь укажет мне, что не так или как это сделать ???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