c# database connection

  • Thread starter Thread starter iulius
  • Start date Start date
I

iulius

Hello,
I get the follwing exception when I'm trying to read data from
SqlDataReader object using GetString(0)
method:System.InvalidCastException: Specified cast is not valid. at
System.Data.SqlClient.SqlDataReader.GetSqlString(Int32 i)!!
If I replace 0 with other index (e.g 1) it's working !!! Why ?
 
I think GetString(0) might be returning a null value,
check with IsDBNull(0) ......

hope this helps,
Wouter
 
Yeh,

I think you should try

if (!reader.IsDBNull (0)) {

// and then if there are casting issues

reader.GerValue (0); // returns an object


}
 
Back
Top