0

私はコードビハインドでこれを持っています:データベースに行を挿入すると挿入は正しいですが、ページを更新すると最後に挿入された行が何度も挿入されます。

 protected void ButtonExecute_Click(object sender, EventArgs e)
        {


            string connectionString =cs.getConnection();
            string insertSql = "INSERT INTO profitCategories(name, IdUser) VALUES(@name, @UserId)";

            using (SqlConnection myConnection = new SqlConnection(connectionString))
            {
                myConnection.Open();
                SqlCommand command = new SqlCommand(insertSql, myConnection);

                command.Parameters.AddWithValue("@name", TextBoxCategory.Text);
                command.Parameters.AddWithValue("@UserId", cui.getCurrentId());
                command.ExecuteNonQuery();
                myConnection.Close();
            }
            TextBoxCategory.Text = string.Empty;

        }

これで問題が解決しました:ページを更新するたびに新しい行が挿入されました

4

1 に答える 1

2

メソッドの最後で、同じページへのリダイレクトを実行します。これにより、POST されたデータが消去されます。

このようなもの:

Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
于 2013-04-03T16:47:51.217 に答える