anil wagavkar

anil wagavkar

  • NA
  • 13
  • 5.6k

how to avoid to insert duplicate data in db.

Mar 23 2016 5:29 AM
when i insert the data i have error exception." The connection was not closed. The connection's current state is open. "
 
here is my code.
 
 
protected void Button3_Click(object sender, EventArgs e)
{
SqlDataAdapter da;
string qry = "select * from salesmans_master where rankuser=" + salesuserid.Text;
da = new SqlDataAdapter(qry, con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
Label15.Text = "this salesid is already present.";
}
else
{
SqlCommand cmd = new SqlCommand("insert into salesmans_master(name,addressof,rankuser,rankcode)values('" + salesname.Text + "','" + salesadderss.Text + "','" + salesuserid.Text + "','" + DropDownList1.SelectedValue + "')", con);
try
{
con.Open();
cmd.ExecuteNonQuery();
Label15.Text = "data Inserted";
con.Close();

}
catch (Exception ex)
{
Label15.Text = ex.Message;
}
}
 

Answers (3)