Below is my registration page and it is allowing duplicate values in the DB.

Could you please help me where am going wrong

private void ExecuteInsert(string UserName, string Password)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        string sql = "INSERT INTO UserMan (UserName, Password) VALUES "
                    + " (@UserName,@Password)";

        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter[] param = new SqlParameter[2];
            //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);

            param[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 20);
            param[1] = new SqlParameter("@Password", SqlDbType.VarChar, 20);

            param[0].Value = UserName;
            param[1].Value = Password;


            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
            }

            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtPassword.Text == txtCnfPassword.Text)
        {
            //call the method to execute insert to the database
            ExecuteInsert(txtUsername.Text,
                          txtPassword.Text);
            Response.Write("Record was successfully added!");
            ClearControls(Page);
        }
        else
        {
            Response.Write("Password did not match");
            txtPassword.Focus();
        }
        Response.Redirect("Login.aspx");
    }

    public static void ClearControls(Control Parent)
    {

        if (Parent is TextBox)
        { (Parent as TextBox).Text = string.Empty; }
        else
        {
            foreach (Control c in Parent.Controls)
                ClearControls(c);
        }
    }

Recommended Answers

All 3 Replies

it is allowing duplicate values in the DB.

Set username to have a unique constraint in your table.

I have created a username with unique constraint in my table and if I give the duplicate value, it is throwing the exception error here. Could you please let me how can I set "User Already exist" message here

catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }

Replace line 5 with a Response.Write for example. The SqlException.Number property has code 2601 when a duplicate key error is triggered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.