J
JB
Hello
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'.
How can I process a datareader record if a field in the record has null
values?
Below is a sample of the problem and the error:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WillUsedProj
{
class WillUsedList
{
private int PoolLoc;
private string Swims;
public WillUsedList(int PoolAddr, string PollHeat)
{
PoolAddr = PoolLoc;
PollHeat = Swims;
}
public int PoolAddr
{
get { return PoolLoc; }
set { PoolLoc = value; }
}
public string PollHeat
{
get { return Swims; }
set { Swims = value; }
}
public static List<object> WillUsedTable()
{
List<object> dtList = new List<object>();
dtList = WillUsedData.GetTblData();
return dtList;
}
}
}
*********************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WillUsedProj
{
class WillUsedData
{
public static List<object> GetTblData()
{
SqlConnection connection = GetConn.GetConnection();
List<object> nuList = new List<object>();
string selectStatement = @"select PoolLoc, Swims from PoolTbl
where Vicinity=10 and
(Swims is null) " + "group by PoolLoc, Swims order by
PoolLoc, Swims ";
SqlCommand selectCommand = new SqlCommand(selectStatement,
connection);
SqlDataReader reader;
connection.Open();
reader =
selectCommand.ExecuteReader(CommandBehavior.SingleResult);
while (reader.Read())
{
WillUsedList WillUsed = new WillUsedList((int)reader["PoolLoc
"],(string)reader["Swims"]);
WillUsed. PoolAddr = (int)reader["PoolLoc"];
WillUsed. PollHeat = (string)reader["Swims"];
nuList.Add(WillUsed.PoolAddr);
nuList.Add(WillUsed. PollHeat);
}
reader.Close();
connection.Close();
return nuList;
}
}
}
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'.
How can I process a datareader record if a field in the record has null
values?
Below is a sample of the problem and the error:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WillUsedProj
{
class WillUsedList
{
private int PoolLoc;
private string Swims;
public WillUsedList(int PoolAddr, string PollHeat)
{
PoolAddr = PoolLoc;
PollHeat = Swims;
}
public int PoolAddr
{
get { return PoolLoc; }
set { PoolLoc = value; }
}
public string PollHeat
{
get { return Swims; }
set { Swims = value; }
}
public static List<object> WillUsedTable()
{
List<object> dtList = new List<object>();
dtList = WillUsedData.GetTblData();
return dtList;
}
}
}
*********************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WillUsedProj
{
class WillUsedData
{
public static List<object> GetTblData()
{
SqlConnection connection = GetConn.GetConnection();
List<object> nuList = new List<object>();
string selectStatement = @"select PoolLoc, Swims from PoolTbl
where Vicinity=10 and
(Swims is null) " + "group by PoolLoc, Swims order by
PoolLoc, Swims ";
SqlCommand selectCommand = new SqlCommand(selectStatement,
connection);
SqlDataReader reader;
connection.Open();
reader =
selectCommand.ExecuteReader(CommandBehavior.SingleResult);
while (reader.Read())
{
WillUsedList WillUsed = new WillUsedList((int)reader["PoolLoc
"],(string)reader["Swims"]);
WillUsed. PoolAddr = (int)reader["PoolLoc"];
WillUsed. PollHeat = (string)reader["Swims"];
nuList.Add(WillUsed.PoolAddr);
nuList.Add(WillUsed. PollHeat);
}
reader.Close();
connection.Close();
return nuList;
}
}
}