0

I'm reading data using sql command and sql reader like

using (var reader = command.ExecuteReader())
{
   if (reader.Read())
   {
         // how can I check is reader["Name"] != null?
        myObj.Name = (string)reader["Name"];
   }
}
1
  • 1
    if (!reader.IsDBNull["Name"]) { Commented Nov 4, 2016 at 11:16

1 Answer 1

1

You can use IsDBNull

if(!reader.IsDBNull("Name")){
    myObj.Name = (string)reader["Name"];
}

Unless you can do as:

if (reader["Name"] != DBNull.Value)
{
   myObj.Name = (string)reader["Name"];
}

OR

myObj.Name  = (reader.IsDBNull("Name")? "": sqlreader.GetString(indexofName));
Sign up to request clarification or add additional context in comments.

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.