Kasuni Abegunawardana

Kasuni Abegunawardana

  • NA
  • 211
  • 124.7k

How to insert a label string value as a float value

Dec 3 2017 3:23 PM
Hi all,
 
I have assigned a values to lable. But i want to insert those values to databse sql. But the data type is float. 
 
This is what i have tried.
I got an error like this.
(0x80131904): Error converting data type nvarchar to float.  
How can i convert these string value lables into float and insert them into data table.
 
Thank you. 
 
 
 
protected void btn_add_Click(object sender, EventArgs e)
{
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",float.Parse(lbl_Calorie.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat));
cmd.Parameters.AddWithValue("@Fat", float.Parse(lbl_fat.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat));
cmd.Parameters.AddWithValue("@Protien", float.Parse(lbl_Protien.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat));
cmd.Parameters.AddWithValue("@Carbohydrates", float.Parse(lbl_Carbohydrates.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat));
cmd.Parameters.AddWithValue("@Cholesterol", float.Parse(lbl_Cholesterol.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat));
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "<script>alert('You are successfully Registered.');</script>");
clear();
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex);
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "<script>alert('Something is going wrong. Please try again.');</script>");
}
finally
{
}
}
 
 
 

Answers (3)