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", "");
clear();
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex);
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "");
}
finally
{
}
}
3 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Rafnas T PPosted Dec 4, 2017, 12:11 AM
Ramesh PalanivelPosted Dec 3, 2017, 11:09 PM
Nilesh ShahPosted Dec 3, 2017, 6:55 PM