Link to home
Start Free TrialLog in
Avatar of Keith McElroy
Keith McElroy

asked on

C# DROP table issue

ASHX C#  problem with DROP TABLE

How can I resolve the error below

SqlCommand listtables = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", conn);
using (SqlDataReader listtablesreader = listtables.ExecuteReader())
{
      while(listtablesreader.Read())
      {
            if(listtablesreader["TABLE_NAME"].ToString().IndexOf("govfin_supplement_") > -1)
            {
                  tabid = listtablesreader["TABLE_NAME"].ToString();
                  context.Response.Write(tabid);
                  SqlCommand droptab = new SqlCommand(@"
                  DROP TABLE [dbo].[" +  tabid +@"]", conn);

                  droptab.ExecuteNonQuery();
                  
                  
            }            
      }
}

Error:

There is already an open DataReader associated with this Command which must be closed first.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed firs
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Keith McElroy
Keith McElroy

ASKER

I ran into a snag with using the sp.  Any thoughts on why CommandType is failing.
If I can solve this, then I can move forward with this little project.


Thanks!


Compiler Error Message: CS0103: The name 'CommandType' does not exist in the current context

Source Error:

Compiler Error Message: CS0103: The name 'CommandType' does not exist in the current context

Source Error:


Line 126:using (SqlCommand cmd = new SqlCommand("sp_RemoveTables", conn))
Line 127:                {
Line 128:                    cmd.CommandType = CommandType.StoredProcedure;
Line 129:                    cmd.ExecuteNonQuery();
Line 130:                }

Source File: c:\inetpub\wwwroot\fin\index.ashx    Line: 128
Never mind, it worked without that line,