jeff lee

jeff lee

  • NA
  • 11
  • 1.1k

stop duplicate record in database

Jun 2 2019 6:14 AM
I need to stop duplicate records in my sql table and if  data exists how can i show message says already exists this is my code below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
 
namespace cap
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-6I0G8AT;Initial Catalog=cap;Integrated Security=True");
con.Open();
string store = "INSERT INTO Customer (Cust_Name,Cust_Username,Cust_Email,Cust_Contact,Cust_Password ) VALUES (@Cust_Name,@Cust_Username,@Cust_Email,@Cust_Contact,@Cust_Password)";
SqlCommand cmd = new SqlCommand(store,con);
cmd.Parameters.AddWithValue("@Cust_NAME", txtfirstname.Text + " " + txtlastname.Text);
cmd.Parameters.AddWithValue("@Cust_Username", txtusername.Text);
cmd.Parameters.AddWithValue("@Cust_Email", txtemail.Text);
cmd.Parameters.AddWithValue("@Cust_Contact", txtcontact.Text);
cmd.Parameters.AddWithValue("@Cust_Password", txtpassword.Text);
cmd.ExecuteNonQuery();
con.Close();
}
}
}

Answers (6)