0

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);
        }
    }
2
Contributors
3
Replies
12
Views
3 Years
Discussion Span
Last Post by pritaeas
0

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();
            }
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.