Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
one textbox and one command button in form. Here i adding Leave_Type(EL/CL). suppose wrongly i have to added ELE instead of EL . how to edit .
database field-leaves.(primary key)
datatypr- varchar
i using below code but its showing error-

C#
Violation of PRIMARY KEY constraint 'PK_Leaves_Type'. Cannot insert duplicate key in object 'dbo.Leaves_Type'. The statement has been terminated.


C#
private void CmdUpdate_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data Source......");
    conn.Open();
    SqlCommand comm = new SqlCommand("update Leaves_Type set Leaves=@Leaves);
	comm.Parameters.AddWithValue("@Leaves",txtltype.Text);    
    comm.Connection = conn;
    comm.ExecuteNonQuery();
    MessageBox.Show("Successfully Updated");
    conn.Close(); 
}


should i use one more field for where condition .
Posted
Updated 26-Apr-14 3:03am
v2

if you used Primary key for the column ,you can't insert the duplicate value to that colunmn

The Primary key won't allow NULL values and Duplicate Values.If you want to Insert duplicate value you must Drop the Primary key constraints
 
Share this answer
 
Check the Leave_Type table. I think Leaves is set as PK column and you are trying to set more rows with same value(There is no where clause applied)
 
Share this answer
 
Well...your update is working, or it's trying to.
It's just that you are trying to update every single row in your table to the same value!
(Think yourself lucky that SQL didn't want to do that because it violates your Primary Key rules or you would have a table full of identical entries - and no way to undo it...)

Try adding a WHERE clause to your SQL to restrict it to a specific row:
C#
SqlCommand comm = new SqlCommand("UPDATE Leaves_Type SET Leaves=@Leaves WHERE MyIdentifyingColumn=@ID");
comm.Parameters.AddWithValue("@Leaves",txtltype.Text); 
comm.Parameters.AddWithValue("@ID", myIdentifingValue);
 
Share this answer
 
v2
Comments
DamithSL 26-Apr-14 12:51pm    
my 5!
Just use below code

private void CmdUpdate_Click(object sender, EventArgs e)
 {
 SqlConnection conn = new SqlConnection(@"Data Source......");
 conn.Open();
 SqlCommand comm = new SqlCommand("update Leaves_Type set Leaves='"+txtltype.Text+"'"); 
    comm.Connection = conn;
 comm.ExecuteNonQuery();
 MessageBox.Show("Successfully Updated");
 conn.Close();
 }


Thanks,
_RG
 
Share this answer
 
Comments
OriginalGriff 26-Apr-14 12:59pm    
Reason for my vote of one: this is identical to the original problem item, *except* it leaves him wide open to SQL Injection, which could damage or destroy his database.<br>
Seriously, you need to read up on Parametrised Queries, and look at your own code - because if you are recommending string concatenation here, you are probably concatenating SQL queries in your own code - which means anyone can do what they like to *your* databases as well...

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



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