W
Web learner
while (dr.Read())
//dr is an instance of sqlDataReader
{
double minAirTemp = (double)(Single)dr["minAirTemp"];
// minAirTemp is column in SQLExpress table (data type: real, allows nulls)
}
The above code works. But when there are NULL, I get the following error
System.InvalidCastException: Specified cast is not valid
To solve this problem, I spent a day on Google. Finally I got a hint from http://www.codeproject.com/cs/database/SmartReader.asp and replaced the code line in while{} as follows:
double minAirTemp1 = (dr.IsDBNull(dr.GetOrdinal("minAirTemp1")))
? 9999 : double.Parse(dr["minAirTemp1"].ToString()); //9999 to respresent nulls
This works for me for now. But just wondering, whether there is a better method to deal with such a situation????
Can someone be kind to point me to some suitable reference !
Thanks !
//dr is an instance of sqlDataReader
{
double minAirTemp = (double)(Single)dr["minAirTemp"];
// minAirTemp is column in SQLExpress table (data type: real, allows nulls)
}
The above code works. But when there are NULL, I get the following error
System.InvalidCastException: Specified cast is not valid
To solve this problem, I spent a day on Google. Finally I got a hint from http://www.codeproject.com/cs/database/SmartReader.asp and replaced the code line in while{} as follows:
double minAirTemp1 = (dr.IsDBNull(dr.GetOrdinal("minAirTemp1")))
? 9999 : double.Parse(dr["minAirTemp1"].ToString()); //9999 to respresent nulls
This works for me for now. But just wondering, whether there is a better method to deal with such a situation????
Can someone be kind to point me to some suitable reference !
Thanks !