SqlDataReader and null fields

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);

}

}
 
M

Morten Wennevik [C# MVP]

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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top