Вопрос

I want to insert a value into a table which comes from a session variable.The code that i have written is:

       protected void Button1_Click(object sender, EventArgs e)
{
    double balance;
    double reward;
    if((double.TryParse(lblBalance.Text, out balance) && (double.TryParse(lblReward.Text, out reward))))
    {
    Session["FinalBalance"] = balance + reward;
    }
    else
    {
// some kind of error handling
    }
    string CS = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into tblRegister('Balance') values('@FinalBalance')", con);
        cmd.Parameters.AddWithValue("@FinalBalance", Session["FinalBalance"].ToString());
    }
}    

But when i click the submit button,the code doesnt throw any exception and doesnt insert the needful.What is the problem here?

Это было полезно?

Решение

You are making the command object but not executing the query. You have to call ExecuteNonQuery method on Command object to insert record.

using (SqlConnection con = new SqlConnection(CS))
{
    con.Open();
    SqlCommand cmd = new SqlCommand("Insert into tblRegister(Balance) values(@FinalBalance)", con);
    cmd.Parameters.AddWithValue("@FinalBalance", Session["FinalBalance"].ToString());
    cmd.ExecuteNonQuery ();
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top