Kasuni Abegunawardana

Kasuni Abegunawardana

  • NA
  • 211
  • 125.1k

How to convert string value to double and insert

Dec 4 2017 7:26 AM
Please can any one say how can i convert string value to double and insert into database i tried but i got an error.
 
double calorie = string.IsNullOrEmpty(lbl_Calorie.Text) ? 1 : double.Parse(lbl_Calorie.Text);
double fat = string.IsNullOrEmpty(lbl_fat.Text) ? 1 : double.Parse(lbl_fat.Text);
double protien = string.IsNullOrEmpty(lbl_Protien.Text) ? 1 : double.Parse(lbl_Protien.Text);
double Carbohydrates = string.IsNullOrEmpty(lbl_Carbohydrates.Text) ? 1 : double.Parse(lbl_Carbohydrates.Text);
double Cholesterol = string.IsNullOrEmpty(lbl_Cholesterol.Text) ? 1 : double.Parse(lbl_Cholesterol.Text);
try
{
// availability();
con.Open();
SqlCommand cmd = new SqlCommand("insert into AddToDiary_tb (U_EmailAddress,Food_Name,Date,Meal_time,Serving_Size,Calories,Fat,Protien,Carbohydrates,Cholesterol) values(@U_EmailAddress,@Food_Name,@Date,@Meal_time,@Serving_Size,@Calories,@Fat,@Protien,@Carbohydrates,@Cholesterol)", con);
cmd.Parameters.AddWithValue("@U_EmailAddress", lbl_userNameDisplay.Text);
cmd.Parameters.AddWithValue("@Food_Name", lbl_FoodName.Text);
cmd.Parameters.AddWithValue("@Date", DateTime.Today.ToString());
cmd.Parameters.AddWithValue("@Meal_time", RadioButtonList1.SelectedValue);
cmd.Parameters.AddWithValue("@Serving_Size",lbl_Size.Text);
cmd.Parameters.AddWithValue("@Calories",calorie);
cmd.Parameters.AddWithValue("@Fat",fat );
cmd.Parameters.AddWithValue("@Protien", protien);
cmd.Parameters.AddWithValue("@Carbohydrates", Carbohydrates);
cmd.Parameters.AddWithValue("@Cholesterol", Cholesterol);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "<script>alert('Successfully added.');</script>");
clear();
GridView1.DataBind();
}
Server Error in '/' Application.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 51: protected void btn_add_Click(object sender, EventArgs e) Line 52: { Line 53: double calorie = string.IsNullOrEmpty(lbl_Calorie.Text) ? 1 : double.Parse(lbl_Calorie.Text); Line 54: double fat = string.IsNullOrEmpty(lbl_fat.Text) ? 1 : double.Parse(lbl_fat.Text); Line 55: double protien = string.IsNullOrEmpty(lbl_Protien.Text) ? 1 : double.Parse(lbl_Protien.Text);

Answers (5)