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
maxdbaseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
The problem is the architecture: Don't do this from your UI. Use stored procedures instead.

And depending on the use-case: Don't do it at all.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maxdbaseAuthor Commented:
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
0
maxdbaseAuthor Commented:
Never mind, it worked without that line,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.