.NET exception with SQL Server 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adarwish
    New Member
    • Dec 2011
    • 9

    .NET exception with SQL Server 2008

    Hi all
    This code runs on IIS.7 in windows server 2008 R2 and MSSQL Server Express Edittion 2008 R2
    this is a simple function which selects from the database based on 3 parameters and resturn object containing the selected row.

    This code runs normally in most time, but in some cases it through exception called "serial_num ber" which is a column name in the database table.
    Although i logged the 3 parameters in case of the exception and they are not null, note that when i simulate the same request with the same parameters in the same server, it runs normally.


    I think in the case of the exception the query runs fine but when getting values of the columns it through this exception, i might be wrong, so please help me.
    Thanks in advance


    Code:
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["meConnectionString"].ConnectionString);
    
            connection.Open();
            SqlCommand cmd;
            using (connection)
            {
                cmd = new SqlCommand("select * from serialnumber_table join countries on  country_id=countryid join applications on ApplicationID=App_ID where phone_number= @phoneNumber and  country_name=@Country and app_name=@app", connection);
                cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
                cmd.Parameters.AddWithValue("@Country", Country);
                cmd.Parameters.AddWithValue("@app", app);
    
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        sn = new PhoneSerialNumber();
                        sn.SerialNumber = rdr["serial_number"].ToString();
                        sn.PhoneNumber = rdr["phone_Number"].ToString();
                        sn.PhoneLang = rdr["Lang"].ToString();
                        sn.PhoneModel = rdr["ModelName"].ToString();
                        sn.ApplicationVersion = rdr["App_Version"].ToString();
                        sn.DealerCode = rdr["dealer_code"].ToString();
                        sn.Size = rdr["size"].ToString();
    
                        TimeSpan Time = DateTime.Parse(rdr["Renewal_Date"].ToString()) - new DateTime(1970, 1, 1, 0, 0, 0);
                        sn._renewal_Date = (long)(Time.TotalMilliseconds);
    
                        rdr.Dispose();
                        connection.Dispose();
                        return sn;
                    }
                }
    
    
                sn = new PhoneSerialNumber();
                sn.SerialNumber = null;
                sn.PhoneNumber = null;
                sn.PhoneModel = null;
                sn.PhoneLang = null;
                sn.ApplicationVersion = null;
                sn.DealerCode = null;
                connection.Dispose();
                return sn;
            }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It would help to see the full error text.

    Comment

    • adarwish
      New Member
      • Dec 2011
      • 9

      #3
      here is the full exception and stack trace

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        The code that generated the error is not in the code that you posted.

        Comment

        • adarwish
          New Member
          • Dec 2011
          • 9

          #5
          full error text:


          Server Error in '/me' Application.

          serial_number

          Description: serial_number

          Exception Details: System.IndexOut OfRangeExceptio n: serial_number

          Source Error:


          Line 61: UpdateMeClass UpdateMeObject = new UpdateMeClass() ;
          Line 62:
          Line 63: string VersionXML = UpdateMeObject. GenereteVersion Tag(Version, PhoneNumber, appName);
          Line 64: context.Respons e.Write("<?xml version=\"1.0\" ?> \n");
          Line 65: context.Respons e.Write(Version XML);

          Source File: c:\inetpub\wwwr oot\me\Handlers \UpdateMe.ashx Line: 63

          Stack Trace:


          [IndexOutOfRange Exception: serial_number]
          System.Data.Pro viderBase.Field NameLookup.GetO rdinal(String fieldName) +2674398
          System.Data.Sql Client.SqlDataR eader.GetOrdina l(String name) +249
          System.Data.Sql Client.SqlDataR eader.get_Item( String name) +23
          PhoneSerialNumb er.GetByPhoneNu mber(String phoneNumber, String Country, String app) +423
          UpdateMeClass.G enereteVersionT ag(String Version, String PhoneNumber, String appName) +534
          UpdateMe.Proces sRequest(HttpCo ntext context) in c:\inetpub\wwwr oot\me\Handlers \UpdateMe.ashx: 63
          System.Web.Call HandlerExecutio nStep.System.We b.HttpApplicati on.IExecutionSt ep.Execute() +599
          System.Web.Http Application.Exe cuteStep(IExecu tionStep step, Boolean& completedSynchr onously) +171

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            I saw that from the link you posted. What I'm saying is that the error message code is not in the code that you posted. Either you posted the wrong code or you posted the wrong error message.

            Comment

            Working...