Unable to cast object of type 'System.Int32' to type 'System.String
This code gives me this exception saying "Unable to cast object of type 'System.Int32' to type 'System.String'." Please help me.Thanks
Code:
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));
}
Re: Unable to cast object of type 'System.Int32' to type 'System.String
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.
Re: Unable to cast object of type 'System.Int32' to type 'System.String
Hi Jm,you're right,it returns an Integer from the table.....so,I used GetInt.
Now,it works good :)
Thanks