samedi 11 juin 2016

Cannot insert duplicate key in object 'dbo.UserInfo'. The duplicate key value is (). The statement has been terminated

In my asp website i have a register form that sends data to the database. When a user enters his data, after submitting, an error is appeared that says:

Violation of PRIMARY KEY constraint 'PK__UserInfo__737584F762CC8D9B'. Cannot insert duplicate key in object 'dbo.UserInfo'. The duplicate key value is (). The statement has been terminated.

This is the code:` signup.aspx.cs-

 protected void submit_Click(object sender, EventArgs e)
{
    string name = Request.Form["name"];
    string email = Request.Form["email"];
    string password = Request.Form["password"];
    string fileName = "Database.mdf";
    string sql = "INSERT INTO UserInfo VALUES('" + name + "','" + email + "','" + password + "')";
    MyAdoHelper.DoQuery(fileName,sql);
}

MyAdoHelper.DoQuery-

public static void DoQuery(string fileName, string sql)
{

    SqlConnection conn = ConnectToDb(fileName);
    conn.Open();
    SqlCommand com = new SqlCommand(sql, conn);
    com.ExecuteNonQuery();
    com.Dispose();
    conn.Close();

}

MyAdoHelper.ConnectToDb-

public static SqlConnection ConnectToDb(string fileName)
{
    string path = HttpContext.Current.Server.MapPath("App_Data/");
    path += fileName;
    //string path = HttpContext.Current.Server.MapPath("App_Data/" + fileName);
    string connString = @"Data Source = (LocalDB)MSSQLLocalDB; AttachDbFilename = C:UsersUserDocumentsVisual Studio 2015WebSitesWebSite6App_DataDatabase.mdf; Integrated Security = True";
    SqlConnection conn = new SqlConnection(connString);
    return conn;

}

Can someone help me? thx!

Aucun commentaire:

Enregistrer un commentaire