-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Sitaraman
Message 4 in Discussion

Hi   Agree with Mahesh.  it wont work even in the v1.0.  In fact, forget execution 
continuing till reader1.read.  The moment you do a reader2=objcommand.executereader(), 
it will throw u an exception with the message "Additional information: There is 
already an open DataReader associated with this Connection which must be closed 
first."    This is because of the validation called internally by the executereader 
method,.  When the exception happens, check your callstack.  it will be as follows     
   system.data.dll!System.Data.SqlClient.SqlCommand::ValidateCommand(String* method = 
"ExecuteReader", bool executing = true) + 0xa8 bytes 
  
system.data.dll!System.Data.SqlClient.SqlCommand::ExecuteReader(System.Data.CommandBehavior
 cmdBehavior = Default, System.Data.SqlClient.RunBehavior runBehavior = 
ReturnImmediately, bool returnStream = true) + 0x93 bytes 
  system.data.dll!System.Data.SqlClient.SqlCommand::ExecuteReader() + 0x16 bytes 
> Dummy_RowState.exe!Dummy_RowState.Module1.subReadData() Line 198 + 0xb bytes Basic
  Dummy_RowState.exe!Dummy_RowState.Module1.main() Line 9 + 0x6 bytes Basic      
ExecutReader() calls -> ExecuteReader(System.Data.CommandBehavior , 
System.Data.SqlClient.RunBehavior,bool returnStream  ) which in turn calls the -> 
ValidateCommand(String* method , bool executing )   The Exception is actually thrown 
by the ValidateCommandmethod. This method internally validates for various things like 
the whether  the  sqlcommand.activeconnection is not nothing or  the  
l_objSQLCommand.activeconnection's state is open or not etc..   It also checks whether 
the connection associated already has any other reader open or not.  Note that 
SQLCommand has a private property , "_activeconnection" which is of connection type, 
pointing to the connectuion the command is currently using.   The connection object in 
turn a friend property, called   "reader",  which is of SQLDataReader type.  This 
property will hold a Open  SQLDataReader object which is using the connection through 
the command object.  So in the following code snippet                  
l_objConnection.Open()
      l_objSQLDataReader1 = l_objSQLCommand1.ExecuteReader()        While 
l_objSQLDataReader1.Read
      Console.writeline(l_objSQLDataReader1.GetString(0))
      End While
       l_objSQLDataReader2 = l_objSQLCommand2.ExecuteReader             At the time of 
connection open the l_objConnection.Reader property will be null         
l_objSQLCommand1.ExecuteReader is executed      validatecommand will check for the   
l_objSQLCommand1._activeconnection.reader, which is nothing and happily the method 
completes       After this ,  the l_objConnection.reader will be made to point to 
l_objSQLDataReader1 (coz this is the reader using the connection)     We read the data 
from l_objSQLDataReader1

        Next, the line l_objSQLDataReader2 = l_objSQLCommand2.ExecuteReader is 
executed         validatecommand will now check for the   
l_objSQLCommand2.activeconnection.reader. Note tht the  
l_objSQLCommand2.activeconnection is the same as  l_objSQLCommand1.activeconnection, 
i.e.  l_objConnection, coz both share the same connection)        Note that in Step 4  
the l_objConnection.reader was made to point to SQLDataReader1 and therefore is NOT 
nothing        So validatecommand will throw an exception which is what you see   To 
avoid this the code can be modified as      l_objConnection.Open()
        l_objSQLDataReader1 = l_objSQLCommand1.ExecuteReader()          While 
l_objSQLDataReader1.Read
            print(l_objSQLDataReader1.GetString(0))
        End While           objSQLDataReader1.close ' Close SQLDataReader1 before 
initializing SQLDataReadwer2 as both of them use the same connection.
        l_objSQLDataReader2 = l_objSQLCommand2.ExecuteReader   Now when the 
SQLDataReader1.close is issued, the SQLDataReader.command.activeconnection.Reader is 
set to nothing. So the executeReader for the l_objSQLDataReader2 is executed, the 
validatecommand does not have any problems and successfully completes...         hth   
regards,   sr    

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to