Reputation: 21
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
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
Reputation: 460108
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