SqlDataReader and null fields

  • Thread starter Thread starter Bill Gower
  • Start date Start date
B

Bill Gower

I am returning data using a SqlDataReader. Here is a snippet of code where
I am having a problem. What is the best way to handle nulls?

private void TGenerateJobSiteList2FromReader<T>(SqlDataReader returnData,
ref List<JobSite> jobSiteList)

{

while (returnData.Read())

{

JobSite jobSite = new
JobSite((string)returnData["JobName"], (int)returnData["JobSiteId"],
(string)returnData["Addr1"], (string)returnData["Addr2"],

(string)returnData["City"],
(string)returnData["state"]);

jobSiteList.Add(jobSite);

}

}
 
I am returning data using a SqlDataReader. Here is a snippet of code where
I am having a problem. What is the best way to handle nulls?

private void TGenerateJobSiteList2FromReader<T>(SqlDataReader returnData,
ref List<JobSite> jobSiteList)

{

while (returnData.Read())

{

JobSite jobSite = new
JobSite((string)returnData["JobName"], (int)returnData["JobSiteId"],
(string)returnData["Addr1"], (string)returnData["Addr2"],

(string)returnData["City"],
(string)returnData["state"]);

jobSiteList.Add(jobSite);

}

}

Hi Bill,

You need to test each field

if(returnData["JobName"] == DBNull.Value)
{
// field is null
}
 
Back
Top