Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi,
this is my code.i have done correctly..but it will show the error as

Error:ExecuteReader requires an open and available Connection. The connection's current state is closed.

C#
public int foundmer(string mername)
        {
            SqlCommand comm1 = new SqlCommand();
            comm1.CommandType = CommandType.Text;
            comm1.CommandText = "select cse from tbl_user_CSE where cse='" + mername + "'";
            comm1.Connection = cn;
            SqlDataReader dtr1;
            dtr1 = comm1.ExecuteReader();
            if (dtr1.HasRows == true)
            {
               dtr1.Close();
                return -1;             
            }
            else
            {
                dtr1.Close();
                return 1;
            }
        }

i dont what is the mistake..can any one tell?
Posted
Updated 3-Oct-18 7:40am
v3

Before Executing any Sql Query make sure that your conection of Application with DataBase is Open.
If cn is your connection object then Add this code before exectuing the Query :
ASP
cn.Open();


I hope it will help you. :)
 
Share this answer
 
Comments
Abhinav S 10-Dec-11 8:17am    
Same as my answer and at the exact same time. 5.
Manoj K Bhoir 11-Dec-11 0:48am    
Thank You Abhinav!
Make sure that cn (your connection) is open.
Before dtr1 = comm1.ExecuteReader(); put the line cn.Open() and then try again.
 
Share this answer
 
Comments
Manoj K Bhoir 11-Dec-11 0:50am    
Yah thats why My 5!
you can change this way.. when you read database first you want to open the connection using [SqlConnectionObject].Open();, In your code you want to add cn.Open();. when you finish data accessing you need to close your connection by [SqlConnectionObject].Close();.

and try to use try-catch-finally[^] for sql operation so you can handle your exception too..

public int foundmer(string mername)
   {
       try
       {
           cn.Open();
           SqlCommand comm1 = new SqlCommand();
           comm1.CommandType = CommandType.Text;
           comm1.CommandText = "select cse from tbl_user_CSE where cse='" + mername + "'";
           comm1.Connection = cn;
           SqlDataReader dtr1;
           dtr1 = comm1.ExecuteReader();
           if (dtr1.HasRows == true)
           {
               dtr1.Close();
               return -1;
           }
           else
           {
               dtr1.Close();
               return 1;
           }
       }
       catch (Exception ex)
       {
           //log your exception here
           //eg: label.Text=ex.Message;
       }
       finally
       { cn.Close(); }
   }
 
Share this answer
 
you forgot to Open the ConnectionState of your SqlConnection. Try adding cn.Open() before dtr1 = comm1.ExecuteReader();

Regards,
Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900