mr.Jack
mr.Jack

Reputation: 13

What is wrong with this class definition?

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

Answers (1)

Akash Kava
Akash Kava

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

Related Questions