-3

i create A page for insert users .

but when i run the the page show me this error

Error Pic

ASP Code

protected void Page_Load(object sender, EventArgs e)
{
    System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["shopDBConnectionString"].ConnectionString;
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandText = "insert into tblmember values ('" + txtemail.Text + "','" + txtname.Text + "','" + txtfamily.Text + "','" + txtpass.Text + "','','" + DropDownList1.SelectedValue + "') ";
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.Connection = con;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    Response.Redirect("Default.aspx");
}

My DataBase

Steve
  • 213,761
  • 22
  • 232
  • 286
Kianoush
  • 39
  • 1
  • 8

1 Answers1

2

It seems you're not passing the primary key in your query, as the duplicate key is shown as Empty (). The Email column Uid (Name is confusing for me) must be unique. You can't have two rows with the same Primary Key.

Secondly and more important, do not concatenate strings from input to build up your query. This can be easily injected with harmful queries that might drop your database.

Check this example on how to use parameterized queries.

Community
  • 1
  • 1
Zein Makki
  • 29,485
  • 6
  • 52
  • 63