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;
}
}
Ravi PatelPosted Mar 23, 2016, 6:08 AM
Rajeev PunhaniPosted Mar 23, 2016, 6:01 AM
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.
Shivanand BastePosted Mar 23, 2016, 5:49 AM