I use this Kind of code for retrieving the data from database.Data are successfully retrieved from database.But when i use this method it produce the error Specified cast is not valid .(Error line is marked as red.).
In the Variable Sun datatype is float.But I don't know why comes error in this line?
private static TimeEntry FillTimeSheet(IDataRecord myDataRecord)
{
TimeEntry myTimeSheet = new TimeEntry();
myTimeSheet.EmpId = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EmployeeId"));
myTimeSheet.EmpName = myDataRecord.GetString(myDataRecord.GetOrdinal("EmployeeName"));
myTimeSheet.Sun = (float)myDataRecord.GetFloat (myDataRecord .GetOrdinal ("sunday"));
myTimeSheet.Mon = myDataRecord.GetFloat(myDataRecord.GetOrdinal("monday"));
myTimeSheet.Tu = myDataRecord.GetFloat(myDataRecord.GetOrdinal("tuesday"));
myTimeSheet.Wed = myDataRecord.GetFloat(myDataRecord.GetOrdinal("wednesday"));
myTimeSheet.Thu = myDataRecord.GetFloat(myDataRecord.GetOrdinal("thursday"));
myTimeSheet.Fri = myDataRecord.GetFloat(myDataRecord.GetOrdinal("friday"));
myTimeSheet.Sat = myDataRecord.GetFloat(myDataRecord.GetOrdinal("saturday"));
myTimeSheet.Tot = myDataRecord.GetInt32(myDataRecord.GetOrdinal("totalhrs"));
return myTimeSheet;
}
prabha ThangaduraiPosted Nov 22, 2007, 7:24 AM
Thanks for your replies.I find out the solution I use the code like,
myTimeSheet.Sun =
float.Parse(myDataRecord ["sunday"].ToString());It works well.Any way thanks
AlanPosted Nov 22, 2007, 7:11 AM
Well, as I'm sure you know, the cast to float is unnecessary as the GetFloat() method already returns a float but, even so, it shouldn't throw an exception :-/
Is it possible that the field could be null which would produce an exception?
You can, of course, check for null values using the IsDBNull() method.
EDIT:
Sorry for the duplication. Got distracted for several minutes in the course of that post and missed SANDIP's reply making the same point as I did.
SANDIPPosted Nov 22, 2007, 7:02 AM
hi,
Are you sure that myDataRecord .GetOrdinal ("sunday") returns you any value?