Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.10/5 (3 votes)
See more:
Hi,
i created login page and one registration form also.In that registration form submit button is there.I wrote this code please check it once and solve my problem.

C#
protected void Button1_Click(object sender, EventArgs e)
{
    con.Open();
    cmd = new SqlCommand("insert into LogIn Values('" + txtfname.Text + "','" + txtlname.Text + "','" + txtuname.Text + "','" + txtpwd.Text + "','" + txtcpwd.Text + "')", con);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("FirstName", txtfname.Text);
    cmd.Parameters.AddWithValue("LastName", txtlname.Text);
    cmd.Parameters.AddWithValue("UserName", txtuname.Text);
    cmd.Parameters.AddWithValue("PassWord", txtpwd.Text);
    cmd.Parameters.AddWithValue("ConfirmPassWord", txtcpwd.Text);
    cmd.ExecuteNonQuery();
    da = new SqlDataAdapter("Select * from LogIn", con);
    ds = new DataSet();
    da.Fill(ds, "LogIn");
    Response.Write("Registration Completed Successfully");
    clear();
    con.Close();
}


Error:
Violation of PRIMARY KEY constraint 'PK_LogIn'. Cannot insert duplicate key in object 'dbo.LogIn'.
The statement has been terminated
Posted
Updated 27-Sep-17 8:31am
v3
Comments
Simon Bang Terkildsen 23-Sep-11 5:45am    
Which column is your primary key?
Thiagarajan Duraisamy 23-Sep-11 5:51am    
giv ur table structure, without which its difficult for us to giv u the reason. To say thinking that username is the primary key in ur table, try tuncating the table and try inserting again.
esther ngaire 20-Mar-21 1:43am    
My Tsc no is 402514.help me to recover password and username

Seems everyone wants you to query the database prior to inserting, do not do that! Assuming username is your primary key, you'll want to catch the exception and then tell the user that the username is already taken.

The reason you don't want to check and then insert is because you'll have what is called a Race Condition.
You might have 2 users creating an account at approximately the smae time, thus your code will run twice on two different threads. Thus you can have a situation where the 2 threads execute the code that checks if the username is taken before either thread inserts a value, so one thread will throw the exception.
 
Share this answer
 
v3
Comments
André Kraak 23-Sep-11 6:12am    
Good answer, my 5.
the user name which you are trying to insert already exists and since it is the primary key, it does not allow duplicates.

before inserting in the database, check the userid already exists or not. If not then insert data.
 
Share this answer
 
Comments
Simon Bang Terkildsen 23-Sep-11 6:01am    
What tells you that the username is the primary key? other than it would be the only sensible key amoung the columns.

Checking if the username already exists before inserting is not a solution, unless you lock the table prior to the check. otherwise someone might insert the username after you've checked for it and before you inserted it. Assuming that username is the primary key, then the OP would want to catch the exception and treat it as if the username is already taken.
Clearly the problem is in the data you are trying to persist. If possible change the value of the field that is declared as primary or remove the record with the key you are trying to insert. Otherwise use an update!


Cheers
 
Share this answer
 
One of your columns you are inserting is a Primary Key and you cannot insert duplicate primary keys (probably UserName).

You have to check first then insert.
 
Share this answer
 
Comments
Simon Bang Terkildsen 23-Sep-11 6:02am    
Checking first and then inserting is not a good solution, you'll have a race condition. See my comment to the first solution.
Mehdi Gholam 23-Sep-11 6:07am    
Agreed, I meant at a higher level ie application level and showing an error etc.
i think username in your table is primary key thats why exception came...
so do one thing ,efore actually inserting the data to table check whether the user exist with that name or not if exist intimate him ..


like this
C#
private bool checkexistornot(string username)
{
con.Open();
   cmd = new SqlCommand("select fname from LogIn where UserName="+username, con);
   cmd.CommandType = CommandType.Text;


   cmd.Parameters.AddWithValue("UserName", txtuname.Text);


   object obj=cmd.ExecuteScalar();

  con.Close();
if(obj!=null)
return false;
else
return true;

}




C#
protected void Button1_Click(object sender, EventArgs e)
{
if(checkexistornot())
{
    con.Open();
    cmd = new SqlCommand("insert into LogIn Values('" + txtfname.Text + "','" + txtlname.Text + "','" + txtuname.Text + "','" + txtpwd.Text + "','" + txtcpwd.Text + "')", con);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("FirstName", txtfname.Text);
    cmd.Parameters.AddWithValue("LastName", txtlname.Text);
    cmd.Parameters.AddWithValue("UserName", txtuname.Text);
    cmd.Parameters.AddWithValue("PassWord", txtpwd.Text);
    cmd.Parameters.AddWithValue("ConfirmPassWord", txtcpwd.Text);
    cmd.ExecuteNonQuery();
    da = new SqlDataAdapter("Select * from LogIn", con);
    ds = new DataSet();
    da.Fill(ds, "LogIn");
    Response.Write("Registration Completed Successfully");
    clear();
    con.Close();
}
else
{
    Response.Write("user with this already exist please select another name");
}
}
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 23-Sep-11 6:04am    
Checking first and then inserting is not a good solution, you'll have a race condition. See my comment to the first solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900