add finally block and then close the reader
On Mon, Jan 4, 2010 at 10:51 PM, Karthikeyan R <[email protected]> wrote:
> Just fixed the problem by closing the reader in the aspx.cs page.
>
> private void gethintno()
> {
> try
> {
> string squery = "select Hint_No from Tbl_Hintquestion where
> Hint_Question = \'" + (Fixquotes(lblhintquestion.Text.Trim())) + "\'";
> IDataReader dr;
> dr = EQuery.ExecuteReader(squery);
> if (dr.Read() == true)
> {
> ihintno = int.Parse(dr["Hint_No"].ToString());
> }
> * dr.Close(); *
> }
> catch (Exception EXE)
> {
> Alert.Show(EXE.Message);
> }
> }
>
> On Mon, Jan 4, 2010 at 6:17 PM, Karthikeyan R <[email protected]>wrote:
>
>> Thanks for your replies.
>>
>> Sushma,
>>
>> As Benj suggested it produced "Unreachable code detected." since return
>> should be last statement in a method (as per my understanding).
>>
>> I have changed the logic and used a datatable to fill the dropdownlist as
>> I found that would be an efficient way rather than using datareader.
>>
>> I have changed the code in the Execute reader method like this,
>>
>> public OleDbDataReader ExecuteReader1(string ReaderQuery)
>>
>> {
>>
>> OleDbDataReader reader = null;
>>
>> try
>>
>> {
>>
>> conn =
>> new OleDbConnection();
>>
>> conn.ConnectionString =
>> DBConnection
>> .GetConnectionString();
>>
>> cmd =
>> new OleDbCommand(ReaderQuery, conn);
>>
>> this.conn.Open();
>>
>> reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
>>
>> }
>>
>> catch (Exception ex)
>>
>> {
>>
>> if (handleErrors)
>>
>> strLastError = ex.Message;
>>
>> else
>>
>> throw;
>>
>> }
>>
>> return reader;
>>
>> }
>> It works good in few scenarios, whereas if I call a method to
>> EXECUTESCALAR after the EXECUTEREADER method it gives the folllowing error,
>> "The connection was not closed. The connection's current state is open."
>> On Mon, Jan 4, 2010 at 6:29 AM, Benj Nunez <[email protected]> wrote:
>>
>>> "*// Try this one Karthik*
>>> *
>>> *
>>> *return dr;*
>>> *then close connection;
>>> *
>>> ** "
>>>
>>>
>>> Hi.
>>>
>>> I think if you do this, you'll get a compiler error/
>>> warning:"Unreachable code detected.".
>>> As far as I can tell, any statement below the return statement
>>> produces this
>>> error/warning except when you program in Borland Delphi. ;)
>>>
>>>
>>>
>>> Cheers!
>>>
>>>
>>>
>>> Benj
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Jan 4, 8:45 am, sushma sushma <[email protected]> wrote:
>>> > *
>>> > Check the red marked code karthikeya.
>>> >
>>> > OleDbDataReader ExecuteReader(string** ReaderQuery)*
>>> >
>>> > *{*
>>> >
>>> > *cmd =*
>>> > *new OleDbCommand**(ReaderQuery, conn);*
>>> >
>>> > *conn.Open();*
>>> >
>>> > *dr = cmd.ExecuteReader(*
>>> > *CommandBehavior**.CloseConnection);*
>>> > *
>>> > *
>>> > *// Try this one Karthik*
>>> > *
>>> > *
>>> > *return dr;*
>>> > *then close connection;
>>> > *
>>> > **
>>> >
>>> > *conn.Close();*
>>> >
>>> > *return** dr;*
>>> >
>>> >
>>> >
>>> > On Sat, Jan 2, 2010 at 7:31 AM, Karthikeyan R <[email protected]>
>>> wrote:
>>> > > Hi,
>>> >
>>> > > I am getting *"Invalid attempt to Read when reader is closed"* error
>>> in
>>> > > one of my pages.I have used the following method in my class file,
>>> >
>>> > > public
>>> > > class ExecuteQuery
>>> >
>>> > > {
>>> >
>>> > > OleDbConnection conn;
>>> >
>>> > > OleDbCommand cmd;
>>> >
>>> > > DataTable dt;
>>> >
>>> > > OleDbDataAdapter da;
>>> >
>>> > > OleDbDataReader dr;
>>> >
>>> > > public ExecuteQuery()
>>> >
>>> > > {
>>> >
>>> > > conn =
>>> > > new OleDbConnection();
>>> >
>>> > > conn.ConnectionString =
>>> > > DBConnection.GetConnectionString();
>>> >
>>> > > }
>>> >
>>> > > *public*
>>> > > * OleDbDataReader ExecuteReader(string** ReaderQuery)*
>>> >
>>> > > *{ *
>>> >
>>> > > *cmd = *
>>> > > *new OleDbCommand**(ReaderQuery, conn); *
>>> >
>>> > > *conn.Open();*
>>> >
>>> > > *dr = cmd.ExecuteReader(*
>>> > > *CommandBehavior**.CloseConnection); *
>>> >
>>> > > *conn.Close();*
>>> >
>>> > > *return** dr;*
>>> >
>>> > > *}*
>>> > > }
>>> >
>>> > > In the aspx.cs file I have used the following code,
>>> >
>>> > > private void ddlfill()
>>> >
>>> > > {
>>> >
>>> > > try
>>> >
>>> > > {
>>> >
>>> > > ddlquestion.Items.Add(
>>> > > "");
>>> >
>>> > > ddlquestion.SelectedIndex = 0;
>>> >
>>> > > string query = "select Hint_Question from Tbl_Hintquestion order by
>>> > > Hint_No ASC";
>>> >
>>> > > bool abc = EQuery.ExecuteReader(query).Read();
>>> >
>>> > > while (abc == true)
>>> >
>>> > > {
>>> >
>>> > > ddlquestion.Items.Add(dr[
>>> > > "Hint_Question"].ToString());
>>> >
>>> > > abc = EQuery.ExecuteReader(query).Read();
>>> >
>>> > > }
>>> >
>>> > > }
>>> >
>>> > > catch (Exception EXE)
>>> >
>>> > > {
>>> >
>>> > > Alert.Show(EXE.Message);
>>> >
>>> > > }
>>> >
>>> > > }
>>> > > Can some one help me out to solve this problem?
>>> > > --
>>> > > Thanks & Regards,
>>> > > Karthikeyan
>>> >
>>> > --
>>> > Thanks,
>>> >
>>> > Sushma
>>> >
>>> > Email: [email protected]
>>>
>>
>>
>>
>> --
>> Thanks & Regards,
>> Karthikeyan
>>
>
>
>
> --
> Thanks & Regards,
> Karthikeyan
>