codify
Member
- Joined
- Dec 4, 2020
- Messages
- 18
- Programming Experience
- 1-3
Hello! I am have connected SQL management studio with my visual studio where I am making this hospital management system. I have set the PATIENT ID as the primary key in the SQL management studio. But when I am entering duplicate id, there is no objection being made by the code. What should I do to not allow the winform to accept duplicate patient id. Below is the code of Patient registration form.
C#:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A85V0ME\SQLEXPRESS;Initial Catalog=Hospitalmanagement;Integrated Security=True");
con.Open();
string gen = string.Empty;
if (radioButton1.Checked)
{
gen = "Male";
}
else
{
gen = "Female";
}
try
{
string str = "INSERT INTO patient(id,name,gen,age,date,cont,addr,disease,status,r_type,building,r_no,price) VALUES('" + textBox1.Text +"','" + textBox2.Text + "','" + gen + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox10.Text + "','" + textBox9.Text + "','" + textBox11.Text + "','" + textBox12.Text + "'); ";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
string str1 = "select max(Id) from patient;";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
MessageBox.Show("Patient Information Saved Successfully..");
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
textBox10.Text = "";
textBox11.Text = "";
textBox12.Text = "";
}
}
catch (SqlException excep)
{
MessageBox.Show(excep.Message);
}
con.Close();
}
private void PatientRegistration_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A85V0ME\SQLEXPRESS;Initial Catalog=Hospitalmanagement;Integrated Security=True");
con.Open();
string str1 = "select max(id) from patient;";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
string val = dr[0].ToString();
if (val == "")
{
textBox1.Text = "1";
}
else
{
int a;
a = Convert.ToInt32(dr[0].ToString());
a = a + 1;
textBox1.Text = a.ToString();
}
}
con.Close();
}