1

I am using the SqlDataReader to get values from an SQL table and assign them to their respective variables. But in my table, int and double type columns have null values. So when I try to read a null int and assign it to an int variable, it crashes.

Here is how the values are being assigned to:

public int ID { get; set; }
public int Map { get; set; }
public int TypeID { get; set; }

And this is where they are being read:

while (objSqlDataReader.Read())
{
    data= new data();
    emissiondata.ID = (int)objSqlDataReader["EFID"];
    emissiondata.Map = (int)objSqlDataReader["EFMappingID"];
    emissiondata.TypeID =(SqlInt32)objSqlDataReader["MobileTypeID"];

So if any of these is null, even when i'm debugging it, it crashes and doesn't continue. How do I handle the null values in SQL and how do I assign empty values to my int if it is null?

5 Answers 5

3

DBNull.Value works on DataTable rows, I'm not sure with SqlDataReader:

bool isNull = objSqlDataReader.GetOrdinal("EFID") == DBNull.Value;

If you use SqlDataReader's GetSqlInt32,GetSqlDateTime, etc for example, they are null-aware out-of-the-box.

Instead of unboxing:

emissiondata.ID = (int)objSqlDataReader["EFID"];

You do this instead:

SqlInt32 emissionId = objSqlDataReader.GetSqlInt32(emissionIdOrdinal);

Then you can test for null directly:

if (emissionId.IsNull) ...

However, those methods need the column's ordinal to access value. You can setup those ordinals before your loop

int emissionIdOrdinal = rdr.GetOrdinal("EFID");

But if you really want C#'s null, you make a helper function for it instead:

public static int? ToNullable(this SqlInt32 value)
{
    return value.IsNull ? (int?) null : value.Value;
}

To access it with your nullable variable:

int? emissionId = objSqlDataReader.GetSqlInt32(emissionIdOrdinal).ToNullable();

You can now test null directly:

if (emissionId == null)

An advice, try to change your class' properties to nullable types, precluding the need for you to use another variable:

public int? ID { get; set; }
public int? Map { get; set; }
public int? TypeID { get; set; }

Final code:

data= new data();
emissiondata.ID = objSqlDataReader.GetSqlInt32(emissionIdOrdinal).ToNullable();
emissiondata.Map = objSqlDataReader.GetSqlInt32(emissionMapOrdinal).ToNullable();
Sign up to request clarification or add additional context in comments.

Comments

2

Can't test it now but you can try with int?, the nullable int. It's a shorthand for Nullable<Int32>. If objSqlDataReader can be cast to int?, change your fields to int? and add extra logic to property setter if you have to do something when value is null.

Comments

2

I realize this is old, but if you are needing this answer after 2016-07-14: When SQL Type is int, null Use int? as the data type.

int? is a "nullable" int - so when SQL returns NULL for a column, the property value receives the value null.

Comments

1

So, to find a possible answer I look at the documentation for the class you're talking about: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx There's a IsDBNull method. Not having C# around, this should work:

while (objSqlDataReader.Read())
{
   data= new data();
   if (!objSqlDataReader.IsDBNull(objSqlDataReader.GetOrdinal("EFID")))
   {
      emissiondata.ID = (int)objSqlDataReader["EFID"];
   } else {
      // Whatever you want to do when it is null
   }
   if (!objSqlDataReader.IsDBNull(objSqlDataReader.GetOrdinal("EFMappingID")))
   {
      emissiondata.Map = (int)objSqlDataReader["EFMappingID"];
   } else {
      // Whatever you want to do when it is null
   }
   if (!objSqlDataReader.IsDBNull(objSqlDataReader.GetOrdinal("MobileTypeID")))
   {
      emissiondata.TypeID = (int)objSqlDataReader["MobileTypeID"];
   } else {
      // Whatever you want to do when it is null
   }
}

Comments

0

You could do this in the query itself. In Oracle, the function is NVL(). Looks like you're using MS Sql Server, which uses:

ISNULL(YOUR_FIELD,0)

Usage and syntax for other RDBMS is here: http://www.w3schools.com/sql/sql_isnull.asp

6 Comments

will this assign my variable as empty or as 0 ?
i was looking to keep it blank if it is null
@Tanny Fgh, I'm not sure what programming language you're using, but you typically can't cast a null to an int primitive.
@TannyFgh, if you need it to be blank, you need to be working with a string data type. It's simply not going to work the way you're casting a null to an int. int is a number. Always.
Well I tried ISNULL(YOUR_FIELD,0) in my query , it still doesnt assign 0 to the variable . It crashes
|

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.