如何停止将重复的用户名添加到数据库表中?

use*_*119 1 c# sql asp.net registration

我正在使用ASP.Net Web应用程序项目.在我的注册表格中Username,Email将检查它是否存在于数据库中.但我的问题是,如果存在username并且Email存在,用户可以正常注册,他的数据将被添加到数据库中!如何阻止它添加这些数据并强制用户更改username或者Email如果其中一个存在!请帮忙吗?

我的.aspx.cs页面:

protected void Button1_Click(object sender, EventArgs e)
        {
            byte[] License;
            Stream s = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(s);
            License = br.ReadBytes((Int32)s.Length);
                try
                {
                    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
                    conn.Open();
                    string insertQuery = "insert into DeliveryMen (Name,Username,Password,Email,Phone,City,License) values (@name ,@username, @password, @email ,@phone ,@city,@License)";
                    SqlCommand com = new SqlCommand(insertQuery, conn);
                    com.Parameters.AddWithValue("@name", TextBoxName.Text);
                    com.Parameters.AddWithValue("@username", TextBoxUsername.Text);
                    com.Parameters.AddWithValue("@password", TextBoxPassword.Text);
                    com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
                    com.Parameters.AddWithValue("@phone", TextBoxPhone.Text);
                    com.Parameters.AddWithValue("@city", DropDownList1.SelectedItem.ToString());
                    com.Parameters.AddWithValue("@License", License);
                    com.ExecuteNonQuery();
                    Response.Write("DONE");
                    conn.Close();
                }
                catch (Exception ex)
                { Response.Write("Error:" + ex.ToString()); }

        }
protected void TextBoxUsername_TextChanged(object sender, EventArgs e)
        { // to check if the Username if exist
            if (!string.IsNullOrEmpty(TextBoxUsername.Text))
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from DeliveryMen where Username=@Username", con);
                cmd.Parameters.AddWithValue("@Username", TextBoxUsername.Text);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    checkusername.Visible = true;
                    imgstatus.ImageUrl = "NotAvailable.jpg";
                    lblStatus.Text = "UserName Already Taken";
                    System.Threading.Thread.Sleep(2000);

                }
                else
                {
                    checkusername.Visible = true;
                    imgstatus.ImageUrl = "Icon_Available.gif";
                    lblStatus.Text = "UserName Available";
                    System.Threading.Thread.Sleep(2000);
                }
            }
            else
            {
                checkusername.Visible = false;
            }

        }

        protected void TextBoxEmail_TextChanged(object sender, EventArgs e)
        { // to check if the Email if exist
            if (!string.IsNullOrEmpty(TextBoxEmail.Text))
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from DeliveryMen where Email=@email", con);
                cmd.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    Div1.Visible = true;
                    Image1.ImageUrl = "NotAvailable.jpg";
                    Label2.Text = "the Email Already Taken";
                    System.Threading.Thread.Sleep(2000);

                }
                else
                {
                    Div1.Visible = true;
                    Image1.ImageUrl = "Icon_Available.gif";
                    Label2.Text = "the Email Available";
                    System.Threading.Thread.Sleep(2000);
                }
            }
            else
            {
                Div1.Visible = false;
            }
        }
Run Code Online (Sandbox Code Playgroud)

Obi*_*Obi 6

在您的用户名和电子邮件列上设置唯一约束,您的sql插入将抛出异常,您可以处理它并相应地通知客户端.

请参阅https://msdn.microsoft.com/en-GB/library/ms190024.aspx