4
Answers

Query creates duplicate entries in SQL server

Mehboob Ali

Mehboob Ali

8y
310
1
 On click below code creates 4 entries of same data.
 
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into class ([className],[mfee],[adfee],[status],[date] )values('" + textBox1.Text + "', '" + textMfee.Text + "', '" + textAdfee.Text + "', '" + comboBox1.SelectedItem.ToString() + "', @Date )", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DateTime ndt = DateTime.ParseExact(textDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
cmd.Parameters.AddWithValue("@Date", ndt);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Class Name already exist");
}
else
{
SqlDataAdapter sda0 = new SqlDataAdapter(cmd);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataTable dt0 = new DataTable();
sda0.Fill(dt0);
MessageBox.Show("New Class added");
}
Answers (4)