Click here to Skip to main content
13,145,603 members (39,362 online)
Rate this:
 
Please Sign up or sign in to vote.
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.

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 10-Dec-11 2:08am
Updated 10-Dec-11 11:55am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 :
cn.Open();


I hope it will help you. :)
  Permalink  
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Make sure that cn (your connection) is open.
Before dtr1 = comm1.ExecuteReader(); put the line cn.Open() and then try again.
  Permalink  
Comments
Manoj K Bhoir 11-Dec-11 0:50am
   
Yah thats why My 5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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(); }
   }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

you forgot to Open the ConnectionState of your SqlConnection. Try adding cn.Open() before dtr1 = comm1.ExecuteReader();

Regards,
Eduard
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 10 Dec 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100