Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (dr.HasRows)
              {
                  while (dr.Read())
                  {
                      list.Add(new VechicleMappingObject()
                      {
                          Map_VechicleCode = Convert.ToInt32(dr["IE_VECHICLE_CODE"].ToString()),
                          Uw_VechicleCode = Convert.ToInt32(dr["UW_VEHICLE_CODE"].ToString()),
                          Map_MakeCode = dr["VEHICLE_MAKE_CODE"].ToString(),
                          Map_ModelCode = dr["VEHICLE_MODEL_CODE"].ToString(),
                          Map_SubTypeCode = dr["VEHICLE_SUBTYPE_CODE"].ToString(),
                          ServiceName = dr["ServiceName"].ToString()
                      });
                  }
              }


its getting the data from the database but when i use 2 add the data 2 the list its was throwing an onject reference is not instance.... pls help me

Thank u in adv
Posted
Updated 11-Sep-13 21:42pm
v2
Comments
ArunRajendra 12-Sep-13 3:40am    
I guess one of the field is having NULL. Hence ToString is throwing object reference error. Identify and handle the NULL data.
Dholakiya Ankit 12-Sep-13 7:01am    
agree
Anurag Sinha V 12-Sep-13 3:44am    
Yep..good point

You may check the value of each nullable column in your DataReader.
For example, suppose that in your database the column ServiceName can be null:
C#
ServiceName = (dr["ServiceName"] != null) ? dr["ServiceName"].ToString() : string.Empty;
 
Share this answer
 
Comments
Faisalabadians 12-Sep-13 9:34am    
Nice one, each row should be checked against dbnull value
phil.o 12-Sep-13 10:56am    
More precisely, each nullable column (ie. DBNull value is accepted by the database engine for this specific column) should be checked for null if any method/property has to be called on the DataReader column value.
For example, if ServiceName was declared as object, it would be OK to write ServiceName = dr["ServiceName"];.
But here, as the ToString() method is called, dr["ServiceName"] must not be null.
C#
if (dr.HasRows)
              {
                  while (dr.Read())
                  {
                      list.Add(new VechicleMappingObject()
                      {
                          Map_VechicleCode = Convert.ToInt32(dr["IE_VECHICLE_CODE"].ToString()),
                          Uw_VechicleCode = Convert.ToInt32(dr["UW_VEHICLE_CODE"].ToString()),
                          Map_MakeCode = dr["VEHICLE_MAKE_CODE"].ToString(),
                          Map_ModelCode = dr["VEHICLE_MODEL_CODE"].ToString(),
                          Map_SubTypeCode = dr["VEHICLE_SUBTYPE_CODE"].ToString(),
                          ServiceName = dr["ServiceName"].ToString()
                      });
                  }
              }
<pre lang="c#">
                          Map_VechicleCode = Convert.ToInt32(dr["IE_VECHICLE_CODE"].ToString()),
                          Uw_VechicleCode = Convert.ToInt32(dr["UW_VEHICLE_CODE"].ToString()),


please check these two column (["IE_VECHICLE_CODE"],["UW_VEHICLE_CODE"]) the value or values are not coming in integer format.
 
Share this answer
 
Comments
ArunRajendra 12-Sep-13 7:57am    
This the duplicate entry. The error is due to no data it can be any field. If it was conversion error then you would got a parsing / convertion exception.
abdussalam143 12-Sep-13 8:18am    
yes you are right NULL is also non integer value but the main reason is conversion failed
ArunRajendra 12-Sep-13 8:28am    
Not necessary conversion is the culprit. even ToString() can cause the error.
abdussalam143 12-Sep-13 8:30am    
agreed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900