none
sqlcommand.endexecutereader exception in .NET 3.5 RRS feed

  • Question

  • I have the following code within a C# function (LoadDefaults):

     

    public
    
     void
    
     LoadDefaults()
    {
    _IsLoading = true ;

    //user roles
    DBobject UserRole = new DBobject();
    SqlConnection UserRoleConn = UserRole.getConnection(true );
    UserRole.CtrlName = "cboRole" ;
    UserRole.AuxilaryRecordsFetched += new AuxilaryRecordsEventHandler(dboObj_AuxilaryRecordsFetched);
    UserRole.FillInfo(UserRoleConn, UserRole.CtrlName, "select ID, role from roles order by role ASC" );

    //locations
    DBobject Locations = new DBobject();
    SqlConnection LocationsConn = Locations.getConnection(true );
    Locations.CtrlName = "cboLocation" ;
    Locations.AuxilaryRecordsFetched += new AuxilaryRecordsEventHandler(dboObj_AuxilaryRecordsFetched);
    Locations.FillInfo(LocationsConn, Locations.CtrlName, "select ID, location from locations order by location ASC" );
    }

    LoadDefaults is an asynchronous function which goes out to the database and fetches various data to pre fill combos on a form, by calling FillInfo various times.

    The FillInfo function looks like this:

     

     public
    
     void
    
     FillInfo(SqlConnection myConnection, string
    
     controlName, string
    
     sqlString)
    {
    ctrl_name = controlName;

    myConnection.Open();
    SqlCommand cmd = new SqlCommand(sqlString, myConnection);
    cmd.CommandType = CommandType.Text;
    cmd.BeginExecuteReader(FillUserData, cmd, CommandBehavior.CloseConnection);
    }

     

    The callback for BeginExecuteReader looks like:

     

     private
    
     void
    
     FillUserData(IAsyncResult result)
    {
    try
    {
    SqlCommand cmd = result.AsyncState as SqlCommand;
    SqlDataReader reader
    reader = cmd.EndExecuteReader(result);
    string [,] tempArray;
    List<string [,]> myList = new List<string [,]>();

    while (reader.Read())
    {
    tempArray = new string [1, 2];
    tempArray[0, 0] = reader.GetValue(0).ToString();
    tempArray[0, 1] = reader.GetValue(1).ToString();
    myList.Add(tempArray);
    }
    CloseConnectionsAndReaders(reader);
    this .ComboArray = myList;
    }
    catch (System.InvalidOperationException ex)
    {
    string str;
    str = "Source:" + ex.Source;
    str += "\n" + "Message:" + ex.Message;
    str += "\n" + "\n" ;
    str += "\n" + "Stack Trace :" + ex.StackTrace;
    MessageBox.Show(str, "Specific Exception" , MessageBoxButtons.OK, MessageBoxIcon.Error);<br/>
      }
    catch (System.Data.SqlClient.SqlException ex)
    {
    string str;
    str = "Source:" + ex.Source;
    str += "\n" + "Message:" + ex.Message;
    MessageBox.Show(str, "Database Exception" , MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    catch (System.Exception ex)
    {
    string str;
    str = "Source:" + ex.Source;
    str += "\n" + "Message:" + ex.Message;
    MessageBox.Show(str, "Generic Exception" , MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }

     

    What happens is that periodically, I will get an unhandled exception error stating the following:

    Source: System.Data

    Message: Internal connection fatal error

    Source:System.Data
    Message:Internal connection fatal error.


    Stack Trace :   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
       at System.Data.SqlClient.SqlDataReader.get_MetaData()
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader()
       at System.Data.SqlClient.SqlCommand.InternalEndExecuteReader(IAsyncResult asyncResult, String endMethod)
       at System.Data.SqlClient.SqlCommand.EndExecuteReader(IAsyncResult asyncResult)
       at IssueTrackerWin.DBobject.FillUserData(IAsyncResult result) in P:\My Documents\IssueTrackerWin\IssueTrackerWin\DBobject.cs:line 177

     

     

     

     

    The line where the error happens is in the FillUserData function: reader = cmd.EndExecuteReader(result);

     

     

     

     

    • Edited by samaklis Tuesday, April 20, 2010 3:44 PM Added error details
    Tuesday, April 20, 2010 1:03 PM

Answers

All replies