Domanda

I Want to handle null values, for the column "ProductState" which returns a string. But isDBNull accepts only integer parameters. What needs to be changed here, please ?

string _productState = "";    

if (!dr.IsDBNull("ProductState"))
                    _productState= dr.GetString("ProductState");
                else
                    _productState= "";
È stato utile?

Soluzione

SqlDataReader.GetOrdinal is your answer

if (!dr.IsDBNull(dr.GetOrdinal("ProductState")))

You could write an extension method to encapsulate this functionality

public static class ReaderExtensions
{
     public static bool IsDBNull(this SqlDataReader reader, string colName)
     {
          return reader.IsDBNull(reader.GetOrdinal(colName));
     }
}

And now you will be able to call the IsDBNull passing a string

if (!dr.IsDBNull("ProductState"))

Looking at your code (reader.GetString("ProductState")) I thought that you are using the MySql provider that supplies an extension GetString (and other GetXXXX) that takes a column name as parameter. But if you have an SqlDataReader then you need to change also that call using the GetOrdinal (or another extensions) because the SqlClient doesn't have a GetString with a column name as parameter

Altri suggerimenti

Try this:

if (!dr.IsDBNull(dr.GetOrdinal("ProductState")))

You can compare it with DBNull.Value like

if(dr["ProductState"] != DBNull.Value)

I use this quick tip:

Casting using as returns null if the field contains DBNull.Value.

if (dr["ProductState"] as string == null)
{
    ...
}

Also, I can use the ?? operator to obtain a fallback value directly in one line if the field contains DBNull.Value

var productState = dr["ProductState"] as string ?? "";

We have a collection of extension methods that can either handle this generically for all column types, or extensions for specific types, which makes the code read like the original set of DataReader methods. So for string, we have these two extensions, which allow for both column ordinal or name, whichever you have access to:

/// <summary>Checks for null before calling GetString.</summary>
/// <returns>The string value of the column, or null if the column is <see cref="DBNull"/>.</returns>
public static string GetNullableString(this IDataRecord row, int i)
{
  object val;
  if((val = row.GetValue(i)) == DBNull.Value)
    return null;
  return val.ToString();
}

/// <summary>Checks for null before calling GetString.</summary>
/// <returns>The string value of the column, or null if the column is <see cref="DBNull"/>.</returns>
public static string GetNullableString(this IDataRecord row, string name)
{
  object val;
  if((val = row[name]) == DBNull.Value)
    return null;
  return val.ToString();
}

and you could modify this to accept a replacement value for null, although we have extension methods that are string specific for that rather than at the DataReader level...again, just to separate concerns:

/// <summary>Checks for null before calling GetString.</summary>
/// <returns>The string value of the column, or <paramref name="defaultVal'/> if the column is <see cref="DBNull"/>.</returns>
public static string GetNullableString(this IDataRecord row, string name, string defaultVal)
{
  object val;
  if((val = row[name]) == DBNull.Value)
    return defaultVal;
  return val.ToString();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top