Reputation: 13
I have a class like this:
class connect:IDisposable
{
public void OpenChannel(SqlConnection ch)
{
ch.ConnectionString=".....";
ch.Open();
}
public void Dispose()
{
}
}
And another class like this:
public Cust
{
SqlConnection channel=new SqlConnection();
SqlCommand command=new SqlCommand();
public void Method()
{
using(connect con=new connect())
{
con.OpenChannel(channel);
command.connection=channel;
.....
....
....
command.ExecuteNonQuery();
}
}
But when I run ExcuteNotQuery() there is an error: "no open connection" So what is wrong?
Upvotes: 1
Views: 116
Reputation: 39916
public Cust
{
SqlConnection channel=new SqlConnection();
SqlCommand command;//=new SqlCommand();
public void Method()
{
using(connect con=new connect())
{
con.OpenChannel(channel);
//command.connection=channel;
// create command from open connection
command = channel.CreateCommand();
.....
....
....
command.ExecuteNonQuery();
}
}
Upvotes: 3