How to Prevent or Avoid Duplicate Record Insertion on Refresh(F5) Click in Browser in ASP.Net ?
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
-
- namespace Student_Management
- {
- public partial class Student_Registration : System.Web.UI.Page
- {
- SqlConnection con = new SqlConnection(@"Data Source=192.168.31.222;Initial Catalog=Student_Database;Integrated Security=True;MultipleActiveResultSets=true;");
-
- protected void Page_Load(object sender, EventArgs e)
- {
- if (con.State == ConnectionState.Open)
- {
- con.Close();
-
- }
-
- con.Open();
-
- }
-
- protected void Button2_Click(object sender, EventArgs e)
- {
- if (TextBox1.Text == String.Empty)
- {
- Label1.Text = "Fill Registration Number" ;
- }
- else if(TextBox2.Text == String.Empty)
- {
- Label1.Text = "Fill Name" ;
- }
- else if (TextBox3.Text == String.Empty)
- {
- Label1.Text = "Fill Qualification" ;
- }
- else if (TextBox4.Text == String.Empty)
- {
- Label1.Text = "Fill Location" ;
- }
- else if (TextBox5.Text == String.Empty)
- {
- Label1.Text = "Fill Phone";
- }
- else if (TextBox6.Text == String.Empty)
- {
- Label1.Text = "Fill Email ID";
- }
- else {
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = "insert into Student_Details values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')";
- cmd.ExecuteNonQuery();
- TextBox1.Text = "";
- TextBox2.Text = "";
- TextBox3.Text = "";
- TextBox4.Text = "";
- TextBox5.Text = "";
- TextBox6.Text = "";
- Response.Write("<script>alert('INSERT NEW STUDENT DATA SUCCESS')</script>");
-
- Label1.Visible = false;
-
- con.Close();
- }
-
-
-
- }
- }
- }