1

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**

2 Answers 2

1

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); 
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.