i have a page that Insert comment by any textbox and test empty values by RequiredFieldValidator.but after insert this values into database and refersh page , again send information to the database.
i tried empty textbox after postback but this not worked and send information again to the database.
Standard practice suggests clearing the textboxes on submit and also validating required fields on the form. This should prevent duplicate submissions.
There must be atleast 1 required field no?
Marked as answer by hiden on Dec 28, 2012 01:23 PM
hiden
Member
69 Points
96 Posts
insert values into table after refresh page
Dec 27, 2012 05:21 PM|LINK
hi to all.
i have a page that Insert comment by any textbox and test empty values by RequiredFieldValidator.but after insert this values into database and refersh page , again send information to the database.
i tried empty textbox after postback but this not worked and send information again to the database.
SqlConnection conect = new SqlConnection(ConfigurationManager.ConnectionStrings["CSCONNECTION"].ConnectionString); if (Page.IsValid) { try { string TxtName = ((TextBox)e.Item.FindControl("TxtNameComment")).Text; string TxtEmail = ((TextBox)e.Item.FindControl("TxtEmailAddressComment")).Text; string TxtBody = ((TextBox)e.Item.FindControl("TxtBodyComment")).Text; conect.Open(); string commandinsertcomment = "sp_Tbl_News_Comment_Insert"; SqlCommand command = new SqlCommand(commandinsertcomment, conect); command.CommandType = CommandType.StoredProcedure; string AddedName = AntiXss.GetSafeHtml(TxtName); string AddedEmail = AntiXss.GetSafeHtml(TxtEmail); string BodyMain = AntiXss.GetSafeHtml(TxtBody); command.Parameters.AddWithValue("NewsID", NewsID); command.Parameters.AddWithValue("AddedName", AddedName); command.Parameters.AddWithValue("AddedIP", Request.UserHostAddress); command.Parameters.AddWithValue("AddedEmail", AddedEmail); command.Parameters.AddWithValue("Body", BodyMain); command.Parameters.AddWithValue("Status", false); command.Parameters.AddWithValue("VotePlus", 0); command.Parameters.AddWithValue("VoteMinus", 0); command.ExecuteReader(); TextBox Name = (TextBox)e.Item.FindControl("TxtNameComment"); TextBox Mail = (TextBox)e.Item.FindControl("TxtEmailAddressComment"); TextBox Body = (TextBox)e.Item.FindControl("TxtBodyComment"); Panel PnlSendMsg = (Panel)e.Item.FindControl("PnlSendMsg"); PnlSendMsg.Visible = false; Name.Text = string.Empty; Mail.Text = string.Empty; Body.Text = string.Empty; Label msgcomment = (Label)e.Item.FindControl("msgcomment"); msgcomment.Visible = true; msgcomment.Text = "send succesfully!"; } catch (Exception ex) { Label msgcomment = ((Label)e.Item.FindControl("msgcomment")); msgcomment.Visible = true; msgcomment.Text = ex.Message; } finally { conect.Close(); } }adamturner34
Contributor
4394 Points
1102 Posts
Re: insert values into table after refresh page
Dec 27, 2012 05:34 PM|LINK
I'm not sure I understand you. Is the insert happening twice?
hiden
Member
69 Points
96 Posts
Re: insert values into table after refresh page
Dec 27, 2012 05:56 PM|LINK
after run top code if user refresh page send information again to server and run top code (Values TextBox stay and not clear).
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: insert values into table after refresh page
Dec 27, 2012 06:20 PM|LINK
Change your stored procedure so that it only inserts records that don't already exist
insert into your table
(field1, field2, etc)
select @value1, @value2, etc
where not exists
(subquery to check for existing records goes here).
This will solve your current problem plus any other relating to duplicate records.
adamturner34
Contributor
4394 Points
1102 Posts
Re: insert values into table after refresh page
Dec 27, 2012 06:34 PM|LINK
Standard practice suggests clearing the textboxes on submit and also validating required fields on the form. This should prevent duplicate submissions.
There must be atleast 1 required field no?