문제

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