PDA

Click to See Complete Forum and Search --> : Unable to cast object of type 'System.Int32' to type 'System.String


uniquegodwin
Feb 7th, 2006, 10:51 PM
This code gives me this exception saying "Unable to cast object of type 'System.Int32' to type 'System.String'." Please help me.Thanks

sqlcmd.CommandText=("Select count(f_activityName) from dbo.tblStandardActivities WHERE (f_activityName IS NOT NULL)");
sread = sqlcmd.ExecuteReader();

while (sread.Read())
{
number_of_activities=Int32.Parse(sread.GetString(0));

}

jmcilhinney
Feb 7th, 2006, 11:10 PM
There are two possible reasons.

1. number_of_activities is a String variable.
2. The column at index 0 contains integer data. You can only successfully call GetString if the field actually contains a string. Otherwise you should forget the parse and call GetInt32.

uniquegodwin
Feb 8th, 2006, 01:16 AM
Hi Jm,you're right,it returns an Integer from the table.....so,I used GetInt.
Now,it works good :)

Thanks