the values are,
int ID,
string name,
float distance..
i wrote this code in my c# DBAccess class.
rdr = com.ExecuteReader();
while (rdr.Read())
{
Merchant merchant = new Merchant();
merchant.ID = rdr.GetInt32(0);
merchant.name= rdr.GetString(1);
merchant.distance= rdr.GetFloat(2);
lstMerchant.Add(merchant);
}
but it gives me an exception "Specified cast is not valid."
This is given by "merchant.distance= rdr.GetFloat(2);" line...
What can i do??
please help me..Thank you.
Kush MuchaalPosted Mar 15, 2013, 5:12 AM
Tharake GunatilakePosted Mar 15, 2013, 4:48 AM
But i got an answer..
merchant.Distance = Convert.ToSingle(rdr[2].ToString());
Thank you for your help Vishal Gilbile..
Vishal GilbilePosted Mar 15, 2013, 4:41 AM
When you read any data through datareader it basically provides data to you in the form of objects, and if your merchant.distance is of float type C# is not able to implicitly convert Object to float, in such cases you should always try to type cast the data in order to avoid error.
merchant.distanct=float.Parse(rdr.GetFloat(2).ToString());
Hope that solves your problem.
With Regards,
Vishal Gilbile.