Groups > Asp .Net > Data Access and ObjectDataSource Control > Re: ExecuteReader requires an open and available Connection. The connection's current state is closed.




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

ExecuteReader requires an open and available Connection. The connection's current state is closed.
Wed, 2 Apr 2008 10:51:38 +0000
Please help this problem..


1    string satirsonu = "SELECT COUNT(*) FROM EkipYer_Tablo";
2    3            SqlCommand cmdSatirSayi = new SqlCommand(satirsonu, sqlCon);
4            SqlDataReader drss =
cmdSatirSayi.ExecuteReader(CommandBehavior.SingleResult);
5            drss.Read();
6    7    string str_ID = drss[0].ToString();
8    int Ekip_IDsi = Convert.ToInt32(str_ID);
9            drss.Close();
10   11   string selectEkip_Id = "SELECT EkipYer_id FROM
EkipYer_tablo";
12           SqlCommand cmdEkip_Id = new SqlCommand(selectEkip_Id, sqlCon);
13           SqlDataReader dr =
cmdEkip_Id.ExecuteReader(CommandBehavior.SingleResult);    
14           dr.Read();
15   16           String EkipYer_ID_deger = dr[Ekip_IDsi].ToString();
17   18           dr.Close();
Post Reply
Re: ExecuteReader requires an open and available Connection. The connection's current state is closed.
Wed, 2 Apr 2008 10:59:47 +0000
You need to call Open() on your sqlCon object before you attempt to read.

If you have a line of code like this

SqlConnection sqlCon = new SqlConnection( ... );

then this only makes a SqlConnection object; it does not open the connection to
the database.

Also remember that you MUST call the connection object's Close() method when
you've finished reading, so at the least you should be using try {} finally {}
blocks to ensure that this is done.
Post Reply
Re: ExecuteReader requires an open and available Connection. The connection's current state is closed.
Wed, 2 Apr 2008 11:04:29 +0000
Thanks very much,,

 I see connection above this code but I forget that in the if condition..
Post Reply
about | contact