This code runs ASP.NET on IIS.7 in windows server 2008 R2 and SQL Server 2008 R2 Express.
This is a simple function which selects from the database based on 3 parameters and returns object containing the selected row.
This code runs normally in most time, but in some cases i get an exception called serial_number
which is a column name in the database table.
This is the complete exception:
[IndexOutOfRangeException: serial_number]
System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName) +2674398
System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) +249
System.Data.SqlClient.SqlDataReader.get_Item(String name) +23
PhoneSerialNumber.GetByPhoneNumber(String phoneNumber, String Country, String app) +423
UpdateMeClass.GenereteVersionTag(String Version, String PhoneNumber, String appName) +534
UpdateMe.ProcessRequest(HttpContext context) in c:\inetpub\wwwroot\me\Handlers\UpdateMe.ashx:63
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171
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.
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;
}