错误“连接未关闭。连接的当前状态是打开的。 [英] Error "The connection was not closed. The connection's current state is open."
问题描述
if(Module1.action ==Add)
{
SqlCommand com = new SqlCommand();
com.CommandText =插入客户值(@ CNo,@ CName,@ Date,@ CAddress,@ Contact_No,@ Deleted) ;
com.Parameters.AddWithValue(@ CNo,txtcustno.Text);
com.Parameters.AddWithValue(@ CName,txtcustname.Text);
com.Parameters.AddWithValue(@ Date,txtdate.Text);
com.Parameters.AddWithValue(@ CAddress,txtcustaddress.Text);
com.Parameters.AddWithValue(@ Contact_No,txtcontact.Text);
//com.Parameters.AddWithValue(\"@UNo, Module1.st);
com.Parameters.AddWithValue(@ D eleted,0);
com.Connection = Module1.con;
try
{
Module1.con.Open();
com.ExecuteNonQuery();
// Module1.con.Open();
//com.ExecuteNonQuery();
// MessageBox.Show(Record saved);
}
catch(exception ex)
{
MessageBox.Show(ex.Message);
}
Module1.con.Close();
fill();
binding();
按钮(! Module1.b);
已锁定(Module1.d);
MessageBox.Show(已保存记录);
// Clientid = txtcustno.Text
return;
}
else if(Module1.action ==Update)
if (Module1.action == "Add")
{
SqlCommand com = new SqlCommand();
com.CommandText = "insert into Customer values(@CNo,@CName,@Date,@CAddress,@Contact_No,@Deleted)";
com.Parameters.AddWithValue("@CNo", txtcustno.Text);
com.Parameters.AddWithValue("@CName", txtcustname.Text);
com.Parameters.AddWithValue("@Date", txtdate.Text);
com.Parameters.AddWithValue("@CAddress", txtcustaddress.Text);
com.Parameters.AddWithValue("@Contact_No", txtcontact.Text);
//com.Parameters.AddWithValue("@UNo", Module1.st);
com.Parameters.AddWithValue("@Deleted", "0");
com.Connection = Module1.con;
try
{
Module1.con.Open();
com.ExecuteNonQuery();
// Module1.con.Open();
//com.ExecuteNonQuery();
// MessageBox.Show("Record saved");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Module1.con.Close();
fill();
binding();
Button(!Module1.b);
Locked(Module1.d);
MessageBox.Show("Record saved");
// Clientid = txtcustno.Text
return;
}
else if (Module1.action == "Update")
推荐答案
一旦连接打开,请确保它已关闭。
once the connection is Open make sure its is closed.
这篇关于错误“连接未关闭。连接的当前状态是打开的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!