Re: How to process datareader nulls coming from datareader?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?UTF-8?B?QXJuZSBWYWpow7hq?=

    #1

    Re: How to process datareader nulls coming from datareader?

    JB wrote:
    I am using a datareader to reader a table. The table may have some null
    values in it. The thing is that I need to process the row whether the field
    has null values in it or not. The "Swim" field in this table will have null
    values in it. The problem is that when I try to read the field with a null
    value I get the error message stating:
    >
    "unable to cast object of type 'System.DBNull' to type 'System.String' .
    reader =
    selectCommand.E xecuteReader(Co mmandBehavior.S ingleResult);
    while (reader.Read())
    {
    WillUsedList WillUsed = new WillUsedList((i nt)reader["PoolLoc
    "],(string)reader["Swims"]);
    if(!reader.IsDB Null(swimcol))
    {
    // use (string)reader[swimcol]
    }
    else
    {
    // use some default value maybe null
    }
    WillUsed. PoolAddr = (int)reader["PoolLoc"];
    WillUsed. PollHeat = (string)reader["Swims"];
    nuList.Add(Will Used.PoolAddr);
    nuList.Add(Will Used. PollHeat);
    }
    reader.Close();
    Arne
  • =?Utf-8?B?SkI=?=

    #2
    Re: How to process datareader nulls coming from datareader?

    Hello Arne

    Actually the error points to the module where the property is set and
    not when the column is read from the datareader. But if you say it is a
    datareader problem, if I am reading a table with lets say for example 25
    columns does that mean write 25 "if" statements (one for each column) or is
    it that the datareader is reading the null column and when it hits the
    propery set module there is some way to deal with it there?
    --
    JB


    "Arne Vajhøj" wrote:
    JB wrote:
    I am using a datareader to reader a table. The table may have some null
    values in it. The thing is that I need to process the row whether the field
    has null values in it or not. The "Swim" field in this table will have null
    values in it. The problem is that when I try to read the field with a null
    value I get the error message stating:

    "unable to cast object of type 'System.DBNull' to type 'System.String' .
    >
    reader =
    selectCommand.E xecuteReader(Co mmandBehavior.S ingleResult);
    while (reader.Read())
    {
    WillUsedList WillUsed = new WillUsedList((i nt)reader["PoolLoc
    "],(string)reader["Swims"]);
    >
    if(!reader.IsDB Null(swimcol))
    {
    // use (string)reader[swimcol]
    }
    else
    {
    // use some default value maybe null
    }
    >
    WillUsed. PoolAddr = (int)reader["PoolLoc"];
    WillUsed. PollHeat = (string)reader["Swims"];
    nuList.Add(Will Used.PoolAddr);
    nuList.Add(Will Used. PollHeat);
    }
    reader.Close();
    >
    Arne
    >

    Comment

    Working...