Click here to Skip to main content
15,902,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have some error in code and i dont understand how to remove it.
double f,g;
string a,b;
           con.Open();
          cmd = new SqlCommand("select MAX (high) FROM DailyChange WHERE Symbol='" + Currency.SelectedValue.ToString() + "' AND Date='" + Label2.Text + "'", con);
          a = cmd.ExecuteScalar().ToString();
          f = Convert.ToDouble(a);
          Response.Write(f);
          con.Close();
          con.Open();
          cmd = new SqlCommand("select MAX (high) FROM DailyChange WHERE Symbol='" + Currency.SelectedValue.ToString() + "' AND Date='" + Label3.Text + "'", con);
          b = cmd.ExecuteScalar().ToString();

          g = Convert.ToDouble(b);
          Response.Write(g);
          con.Close();

It gives me the following error:
Input string was not in a correct format.
Line 81: g = Convert.ToDouble(b);
Posted
Updated 25-Jul-11 0:36am
v2

Are you sure "b" has a value that can be converted to double?

Update: There are tow problems in your code. This is how it should be IMHO:

object b = cmd.ExecuteScalar();

double result;

if((b != null) && double.TryParse(b.ToString(), out result){

// Do something with result

}
else{
// Result is either null or not a double value. Proceed accordingly
}
 
Share this answer
 
v2
Comments
saifullahiit 25-Jul-11 6:41am    
yes
dan!sh 25-Jul-11 6:45am    
What value does it have?
nareshgundapaneni 25-Jul-11 6:54am    
Sure b value will be string only that is also like"abc" or "ABC" like this that's why it is giving that error man
dan!sh 25-Jul-11 7:00am    
That is very much evident from the code. Still what that string value has is unclear.
Sergey Alexandrovich Kryukov 25-Jul-11 11:04am    
Right, my 5.
--SA
Please check what are you receiving in b by debugging and make sure you are getting something like "1.3" or "1" etc. not like "abc" because this will always give you the FormatException: Input string was not in a correct fromat.

hope it helps :)

for further queries comment here!!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jul-11 11:05am    
Right, but also, TryParse should be used as d@nish has shown. (I voted 4.)
--SA
hi,
try
{

g = Convert.ToDouble(b);
}
catch(Exception)
{
}

write the remaining code.
regards,
shefeek
 
Share this answer
 
v2

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



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