在下面的代码中,我正在执行在 db 中插入值的功能。一切正常,但问题是如果我按 F5 或 Ctrl+F5,页面将重新加载,并且再次插入相同的值。我刚刚清空了文本框的值,但它不起作用。如何防止重复插入..
提前致谢。
protected void btnAddNewQuestion_Click(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("Usp_insertNewQuestion", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@AppID", SqlDbType.Int).Value = applicationId;
cmd.Parameters.Add("@TenantID", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@Questions", SqlDbType.VarChar).Value = txtNewQuestion.Text;
cmd.Parameters.Add("@QuestionType", SqlDbType.VarChar).Value = ddlNewQuestionType.Text;
cmd.Parameters.Add("@AudioPath", SqlDbType.VarChar).Value = txtNewAudioPath.Text;
cmd.Parameters.Add("@QuestionStatus", SqlDbType.Int).Value = Int32.Parse(rdoNewQuestionStatus.SelectedItem.Value);
cmd.Parameters.Add("@DataType", SqlDbType.VarChar).Value = ddlNewQuestionDataType.Text;
cmd.Parameters.Add("@UserField", SqlDbType.VarChar).Value = ddlNewUserField.Text;
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
Response.Write(err.Message);
}
finally
{
con.Close();
}
//string type = Page.Request.Form["hdnAddQuestionField"].ToString();
// if (type == "Security Question")
// getSecurityQuestions();
//if (type == "Personal Identity")
// getPersonalIdentityQuestions();
//if (type == "Past History")
// getPastHistoryQuestions();
txtNewQuestion.Text = string.Empty;
ddlNewQuestionType.Text = string.Empty;
txtNewAudioPath.Text =string.Empty;
rdoNewQuestionStatus.SelectedItem.Value = string.Empty;
ddlNewQuestionDataType.Text = string.Empty;
ddlNewUserField.Text = string.Empty;
}