Tech Off Thread

14 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Intermittent exceptions raised by SqlClient

Back to Forum: Tech Off
  • User profile image
    W3bbo

    I've been recording these exceptions intermittently occuring (I got the stacktraces by getting the program to email them to me).

    These two exceptions occur at fairly random intervals:

    Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)

    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)

    at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)

    at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)

    at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)

    at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)

    at W3b.AMS.DalSql.Containers.InternalContainerGet(String CompositeName, Int64 ContainerId, Boolean GetUnpublished, Int32 Page, Int32 RecsPerPage) in C:\Documents and Settings\David\My Documents\Visual Studio Projects\W3b\Solutions\W3b.AMS\W3b.AMS.DalSql\DalBases\Containers.cs:line 58

    at W3b.AMS.DalSql.Containers.ContainerGet(String CompositeName, Boolean GetUnpublished, Int32 Page, Int32 RecsPerPage) in C:\Documents and Settings\David\My Documents\Visual Studio Projects\W3b\Solutions\W3b.AMS\W3b.AMS.DalSql\DalBases\Containers.cs:line 18

    at W3b.AMS.Lib.DAL.Bal.ContainerGet(String CompositeName, Boolean GetUnpublished, Int32 Page, Int32 RecsPerPage) in C:\Documents and Settings\David\My Documents\Visual Studio Projects\W3b\Solutions\W3b.AMS\W3b.AMS.Lib\DAL\Bal.cs:line 517

    at W3b.AMS.Pages.Default.LoadContainer(String CompositeName, String Args) in z:\proj\ams\default.aspx.cs:line 175

    at W3b.AMS.Pages.Default.LoadPage(String CompositeName, String Args, PageMode PM) in z:\proj\ams\default.aspx.cs:line 132


    and
    Object reference not set to an instance of an object.


    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)

    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)

    at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)

    at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)

    at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)

    at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
    at W3b.AMS.DalSql.Base.ExecuteSet(String SprocName, Param[] Params) in C:\Documents and Settings\David\My Documents\Visual Studio Projects\W3b\Solutions\W3b.AMS\W3b.AMS.DalSql\Base.cs:line 91

    at W3b.AMS.DalSql.Articles.InternalArticleGet(Int64 i64ArtId, String strCompositeName) in C:\Documents and Settings\David\My Documents\Visual Studio Projects\W3b\Solutions\W3b.AMS\W3b.AMS.DalSql\DalBases\Articles.cs:line 45

    at W3b.AMS.DalSql.Articles.ArticleGet(String strCompositeName) in C:\Documents and Settings\David\My Documents\Visual Studio Projects\W3b\Solutions\W3b.AMS\W3b.AMS.DalSql\DalBases\Articles.cs:line 17

    at W3b.AMS.Lib.DAL.Bal.InternalArticleGet(Int64 ArticleId, String CompositeName) in C:\Documents and Settings\David\My Documents\Visual Studio Projects\W3b\Solutions\W3b.AMS\W3b.AMS.Lib\DAL\Bal.cs:line 352

    at W3b.AMS.Lib.DAL.Bal.ArticleGet(String CompositeName) in C:\Documents and Settings\David\My Documents\Visual Studio Projects\W3b\Solutions\W3b.AMS\W3b.AMS.Lib\DAL\Bal.cs:line 299

    at W3b.AMS.Pages.Default.LoadArticle(String CompositeName, Boolean ErrorPage, String ErrorMessage, String Args) in z:\proj\ams\default.aspx.cs:line 225

    at W3b.AMS.Pages.Default.LoadArticle(String CompositeName, String Args) in z:\proj\ams\default.aspx.cs:line 220

    at W3b.AMS.Pages.Default.LoadPage(String CompositeName, String Args, PageMode PM) in z:\proj\ams\default.aspx.cs:line 128

    The source that causes the first exception is here (emphasised line):

            private DataSet InternalContainerGet(string CompositeName, Int64 ContainerId, bool GetUnpublished, Int32 Page, Int32 RecsPerPage) {
                
                SqlCommand S = new SqlCommand("ams_ContainerGet", c);
                S.CommandType = CommandType.StoredProcedure;
                
                if(CompositeName.Length < 1) {
                    S.Parameters.Add("@CompositeName", DBNull.Value);
                } else {
                    S.Parameters.Add("@CompositeName", CompositeName);
                }
                
                if(RecsPerPage != Int32.MinValue) {
                    S.Parameters.Add("@RecsPerPage", RecsPerPage);
                } else {
                    S.Parameters.Add("@RecsPerPage", DBNull.Value);
                }
                
                if(Page != Int32.MinValue) {
                    S.Parameters.Add("@Page", Page);
                } else {
                    S.Parameters.Add("@Page", DBNull.Value);
                }
                
                S.Parameters.Add("@ContainerId", ContainerId);
                S.Parameters.Add("@GetUnpublished", GetUnpublished);
                
                SqlDataAdapter A = new SqlDataAdapter(S);
                
                DataSet DS = new DataSet();
                
                A.Fill(DS);
                
                A.Dispose();
                S.Dispose();
                
                if(DS.Tables.Count < 1 || DS.Tables[0].Rows.Count != 1) {
                    DS.Dispose();
                    return null;
                }
                
                return DS;
                
            }
    


    And the second exception:

            protected DataSet ExecuteSet(string SprocName, Param[] Params) {
                
                SqlCommand S = new SqlCommand(SprocName, c);
                S.CommandType = CommandType.StoredProcedure;
                
                if (Params != null) {
                    int cnt = Params.GetUpperBound(0) + 1;
                    for (int i = 0; i < cnt; i++) {
                        S.Parameters.Add(Params[i].Name, Params[i].Value);
                    }                
                }
                
                DataSet DS = new DataSet();
                
                SqlDataAdapter A = new SqlDataAdapter(S);
                A.Fill(DS);
                
                A.Dispose();
                S.Dispose();
                
                return DS;
                
            }
    


    (I'll be converting the InternalContainerGet method into a call to ExecuteSet after I sort this problem out BTW).

    I saw this Microsoft Support article, but whilst it describes the problem, I don't think it's appropriate (besides, wasn't this fixed in .NET1.1 SP1?)

    Any ideas?

  • User profile image
    Maurits

    Add a check to see if (A == null)

  • User profile image
    W3bbo

    Maurits wrote:

    Add a check to see if (A == null)



    But why is that necessary? I would have gotten an NRE immediately after I called the .Fill() method.

  • User profile image
    Maurits

    W3bbo wrote:
    I saw this Microsoft Support article, but whilst it describes the problem, I don't think it's appropriate (besides, wasn't this fixed in .NET1.1 SP1?)


    What version is your System.data.dll?

  • User profile image
    W3bbo

    Maurits wrote:
    W3bbo wrote:I saw this Microsoft Support article, but whilst it describes the problem, I don't think it's appropriate (besides, wasn't this fixed in .NET1.1 SP1?)


    What version is your System.data.dll?


    On the servers: 1.1.4322.2300
    On my development computer: 1.1.4322.2032

    The application is going to be distributed though, how can I ensure my users are running the patched version?

    EDIT: The article says the fixed version is "1.1.4322.1007", and I'm clearly running more recent versions.

  • User profile image
    cheong

    I suggest to add a breakpoint before S.Parameters to see if it matches the number of parameter of the storeproc.

    Also, I wonder if add a check for "the state of SQL connection" before A.Fill() will help. I'm thinking of the possibility to have more than one database operation occur at the same time, or have the connection timeout during operation(network problem maybe). Although the CLR ought to have a check on this and should return more meaningful message, it doesn't hurt to check.

    Just some wild guess, through...

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    W3bbo

    cheong wrote:
    I suggest to add a breakpoint before S.Parameters to see if it matches the number of parameter of the storeproc.


    Then it would throw a SqlException with a message about parameters.

    cheong wrote:
    Also, I wonder if add a check for "the state of SQL connection" before A.Fill() will help. I'm thinking of the possibility to have more than one database operation occur at the same time, or have the connection timeout during operation(network problem maybe). Although the CLR ought to have a check on this and should return more meaningful message, it doesn't hurt to check.

    Just some wild guess, through...


    then I'd get a SqlException about the ConnectionState.

    I've seen both of those exception types before, the problem I'm experiencing is totally unique. I'm going to phone Microsoft up.

  • User profile image
    Maurits

    W3bbo wrote:
    the problem I'm experiencing is totally unique. I'm going to phone Microsoft up.


    Good idea... let us know.

  • User profile image
    W3bbo

    Maurits wrote:
    W3bbo wrote:the problem I'm experiencing is totally unique. I'm going to phone Microsoft up.


    Good idea... let us know.


    It's 5:30PM, I'll have to phone them tomorrow.

  • User profile image
    W3bbo

    Update: I just phoned MS and got through to their developer support department.

    Whilst the problem is with .NET 1.1 I've used up both my support incidents with my VS2003, so I gave him my VS2005 info (thankfully he let me off, of course can can technically use VS2005 to develop 1.1 apps, and the issue is with the framework, not my IDE... ah well)

    BTW, Note to Microsoft: 2 support incidents isn't enough, consider raising it to a complimentary pack of 5 with stand-alone copies of VS.

    so anyway, an incident has been filed and I've got a questionaiire sitting in my inbox waiting to be filled in.

    I'll keep you informed.

  • User profile image
    ZippyV

    Why do you write this?

    if(CompositeName.Length < 1) {

    instead of

    if (CompositeName == "") {

  • User profile image
    Maurits

    ZippyV wrote:
    Why do you write this?

    if(CompositeName.Length < 1) {

    instead of

    if (CompositeName == "") {


    The latter requires creating an extra unnecessary string object.

  • User profile image
    PerfectPhase

    Maurits wrote:
    ZippyV wrote: Why do you write this?

    if(CompositeName.Length < 1) {

    instead of

    if (CompositeName == "") {


    The latter requires creating an extra unnecessary string object.


    Not if you use string.Empty, infact I'm sure the compiler converts "" to string.Empty anyway.

    EDIT: BTW the real reason it's fast is that the operator overloading for the string type does not have to be resolved when you do a compare on the string length vs the string it's self, makes the compare about twice as fast.

    One other thing by using s == string.Empty you survive this:

    string s = null;
    if (s.Length == 0)
    {
    }

  • User profile image
    Maurits

    PerfectPhase wrote:
    I'm sure the compiler converts "" to string.Empty anyway.


    I would hope so Smiley

    if (s == null || s == "") // v1.1
    if (string.IsNullOrEmpty(s)) // v2

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.