简体   繁体   中英

How to avoid record duplication

If I try to submit the Employee into the database whose email address already exists in the database, I want to throw the Message box to let the user know of the issue, otherwise let the data be saved.

Down below is my current code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Employees
{
    public partial class Registration : Form
    {
        SqlConnection con;
        SqlCommand com;
        SqlDataReader sdr;
        string cmdstr;

        string constr = @"Data Source=DESKTOP-J6KRI77\SQLEXPRESS; Initial Catalog = SELLnBUY; Integrated Security=true";


        public Registration()
        {
            InitializeComponent();
        }

 private void btnsubmit_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(constr);
            cmdstr = "INSERT INTO Emptbl([First Name], [Last Name], sex, [Birth Date], Email, Password, Confirm_Password, Membership) VALUES(@fname, @lname, @sex, @dob, @email, @password, @confirmpassword, @membership)";

            con.Open();
            com = new SqlCommand(cmdstr, con);

            if (txtfname.Text == "" || txtlname.Text == "" || combosex.SelectedItem.ToString() == "" || maskeddob.Text == "" || txtemail.Text == "" || txtpassword.Text == "" || txtconfirmpassword.Text == "" || (!radiopremium.Checked && !radioregular.Checked))
            {
                MessageBox.Show("Please, fill all the fields");
            }

            else
            {
                com.Parameters.AddWithValue("@fname", txtfname.Text);
                com.Parameters.AddWithValue("@lname", txtlname.Text);
                com.Parameters.AddWithValue("@sex", combosex.SelectedItem.ToString());
                com.Parameters.AddWithValue("@dob", maskeddob.Text);
                com.Parameters.AddWithValue("@email", txtemail.Text);
                com.Parameters.AddWithValue("@password", txtpassword.Text);
                com.Parameters.AddWithValue("@confirmpassword", txtconfirmpassword.Text);
                if (radiopremium.Checked)
                {
                    com.Parameters.AddWithValue("@membership", "Premium");
                }
                else
                {
                    com.Parameters.AddWithValue("@membership", "Regular");
                }
                com.ExecuteNonQuery();

                MessageBox.Show("Thanks for registering");
                txtfname.Clear();
                txtlname.Clear();
                combosex.Text = "";
                maskeddob.Clear();
                txtemail.Clear();
                txtpassword.Clear();
                txtconfirmpassword.Clear();
                grpbxmembership.Text = "";

                txtfname.Focus();

            }
con.Close();
}

First, your code is very old. All people use ORMs nowadays.

Then, about your question, you just need to check DB for the same data before you insert it.

SqlCommand command = new SqlCommand("select count(*) from Emptbl where  Email= @email");
  command.Parameters.AddWithValue("@email", txtemail.Text);
  var count= (Int32)cmd.ExecuteScalar();
   if(count>0) 
       //Show error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM