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;
}
}

Shivanand Baste31 reputationPosted Mar 23, 2016, 1:49 AM EDT
Rajeev PunhaniRank #551 · 4.4K reputationPosted Mar 23, 2016, 2:01 AM EDT
Hi Anil,
Why are you opening connection for filling dataset.It will open connection itself and close it.So yo are getting error while trying to open for SqlCommand.
If the above solution is helpful then,accept the answer.
Ravi PatelRank #439 · 5.7K reputationPosted Mar 23, 2016, 2:08 AM EDT