duplicate record is created in database on page reload

Mar 30 2020 3:16 AM
How to Prevent or Avoid Duplicate Record Insertion on Refresh(F5) Click in Browser in ASP.Net ?
 
  1. using System;  
  2. using System.Text;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10.   
  11. namespace Student_Management  
  12. {  
  13.     public partial class Student_Registration : System.Web.UI.Page  
  14.     {  
  15.         SqlConnection con = new SqlConnection(@"Data Source=192.168.31.222;Initial Catalog=Student_Database;Integrated Security=True;MultipleActiveResultSets=true;");  
  16.   
  17.         protected void Page_Load(object sender, EventArgs e)  
  18.         {  
  19.             if (con.State == ConnectionState.Open)  
  20.             {  
  21.                 con.Close();  
  22.                // Response.Redirect(Request.Url.ToString(), false);  
  23.             }  
  24.   
  25.             con.Open();  
  26.               
  27.         }  
  28.   
  29.         protected void Button2_Click(object sender, EventArgs e)  
  30.         {  
  31.             if (TextBox1.Text == String.Empty)  
  32.             {  
  33.                 Label1.Text = "Fill Registration Number" ;  
  34.             }  
  35.             else if(TextBox2.Text == String.Empty)  
  36.             {  
  37.                 Label1.Text = "Fill Name" ;  
  38.             }  
  39.             else if (TextBox3.Text == String.Empty)  
  40.             {  
  41.                 Label1.Text = "Fill Qualification" ;  
  42.             }  
  43.             else if (TextBox4.Text == String.Empty)  
  44.             {  
  45.                 Label1.Text = "Fill Location" ;  
  46.             }  
  47.             else if (TextBox5.Text == String.Empty)  
  48.             {  
  49.                 Label1.Text = "Fill Phone";  
  50.             }  
  51.             else if (TextBox6.Text == String.Empty)  
  52.             {  
  53.                 Label1.Text = "Fill Email ID";  
  54.             }  
  55.             else {  
  56.                 SqlCommand cmd = con.CreateCommand();  
  57.                 cmd.CommandType = CommandType.Text;  
  58.                 cmd.CommandText = "insert into Student_Details values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')";  
  59.                 cmd.ExecuteNonQuery();  
  60.                 TextBox1.Text = "";  
  61.                 TextBox2.Text = "";  
  62.                 TextBox3.Text = "";  
  63.                 TextBox4.Text = "";  
  64.                 TextBox5.Text = "";  
  65.                 TextBox6.Text = "";  
  66.                 Response.Write("<script>alert('INSERT NEW STUDENT DATA SUCCESS')</script>");  
  67.                   
  68.                 Label1.Visible = false;  
  69.   
  70.                 con.Close();  
  71.             }  
  72.               
  73.   
  74.   
  75.         }  
  76.     }  
  77. }  
 

Answers (2)