Click here to Skip to main content
15,885,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can Anyone know this, ans me.......

Error code:


C#
public float getshipping(string deliveryarea)
{
    SqlCommand cmd3 = new SqlCommand("select shipping from delivery where status='A' and area='" + deliveryarea + "'", con);
    SqlDataReader dr3;
    dr3 = cmd3.ExecuteReader();
    while (dr3.Read())
    {
        shipping = float.Parse(dr3[0].ToString());
    }
    dr3.Close();
    return shipping;
}
Posted
Updated 27-Nov-11 22:10pm
v2
Comments
DaveAuld 28-Nov-11 4:15am    
On a side note, suggest you look at converting your code to parameterised queries.
raj ch 28-Nov-11 4:28am    
May be the value ur trying to convert is not in correct format
Nickos_me 28-Nov-11 5:17am    
Little advise - it's better to use SqlParameter instead of "select shipping from delivery where status='A' and area='" + deliveryarea + "'".
And better use using instead of dr3.Close();

You are trying to convert a value to float..Check what value you are getting in dr3[0].
 
Share this answer
 
You are trying to parse the value in float that might bot be digits.
pl. check the value of dr3[0].ToString()
 
Share this answer
 
C#
public float getshipping(string deliveryarea)
  {
      float shipping =0;
      SqlCommand cmd3 = new SqlCommand("select shipping from delivery where status='A' and area='" + deliveryarea + "'", con);
      SqlDataReader dr3;
      dr3 = cmd3.ExecuteReader();
      while (dr3.Read())
      {
          float.TryParse(Convert.ToString(dr3[0]),out shipping);
      }
      dr3.Close();
      return shipping;
  }


Instead of float.Parse you can use float.TryParse which will return 0 if there is invalid value.
 
Share this answer
 
v2
C#
shipping = float.Parse(dr3[0].ToString());


debug it.. check if dr3[0] is convertible to float.. if not, then that is the cause of the error

-Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900