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