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.

Vishal GilbileRank #268 · 9.4K reputationPosted Mar 15, 2013, 12:41 AM EDT
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.
Tharake Gunatilake43 reputationPosted Mar 15, 2013, 12:48 AM EDT
But i got an answer..
merchant.Distance = Convert.ToSingle(rdr[2].ToString());
Thank you for your help Vishal Gilbile..
Kush MuchaalRank #2,573 · 543 reputationPosted Mar 15, 2013, 1:12 AM EDT