CS 2014
CS 2014

Reputation: 21

Null Values from query in List

I have a Web service that receive one list, in some cases some columns can be null and when I try insert Null values in my List I got a error. "Column is null". How I Can Insert NULL value in list if some column is null??

  dr = cmd.ExecuteReader();
  List<ClientData> myList = new List<ClientData>();
  while (dr.Read())
   {
     ClientData client = new ClientData();
     client.clientId = dr.GetString(0);
     client.ClientName = dr.GetString(1); **---> NULL VALUE**

Upvotes: 1

Views: 108

Answers (2)

nvoigt
nvoigt

Reputation: 77304

You can check if it is null before calling a conversion method:

client.ClientName = dr.IsDBNull(1) ? null : dr.GetString(1); 

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460108

Use DbDataReader.IsDBNull

while (dr.Read())
{
 ClientData client = new ClientData();
 client.clientId = dr.GetString(0);
 if(dr.IsDbNull(1))
     client.ClientName = null; 
 else
     client.ClientName = dr.GetString(1); 

Upvotes: 1

Related Questions