Click here to Skip to main content
15,885,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting error as "cannot update ID" in update table.plz help me resolving this

What I have tried:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Submit")
{
using (OleDbConnection con1 = new OleDbConnection(con))
{
con1.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con1;
//string query1 = ("insert into Reports (ID,Comments,marks,Reviewer,ReviewerDate) values(@id,@comments,@marks,'" + Environment.UserName + "',Now)");
string query1 = ("Update Reports set ID = @id,Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where(ID = @id)");
cmd.Parameters.AddWithValue("@id", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@comments", dataGridView1.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@marks", dataGridView1.Rows[i].Cells[6].Value);
cmd.Parameters.AddWithValue("@rvr", dataGridView1.Rows[i].Cells[8].Value);
cmd.Parameters.AddWithValue("@rvrd", dataGridView1.Rows[i].Cells[9].Value);

cmd.CommandText = query1;
cmd.ExecuteNonQuery();

}
MessageBox.Show("Comments Submitted", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Posted
Updated 7-Jun-18 5:00am

In addition to what Nathan says, think about it: even if you could update the ID value, is there any point?
Update Reports set ID = @id,Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where(ID = @id)
Given that the WHERE clause ensures that only rows with the ID set to a specific value will be altered, what effect would trying to set the ID to that specific value have?
 
Share this answer
 
Comments
Prateek gsharma 8-Jun-18 0:53am    
string query1 = ("Update Reports set Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where (ID = @id)");

In the access database
ID data type is AutoNumber
ID field size Long Integer
New values : Increment


But above query is not making any changes to perticular column/Row
is there any other query to make changes in database?
Prateek gsharma 8-Jun-18 7:03am    
moreover i am using this query in Datagridview where i have change the values & update it in database.
Because the ID is the row identity, you're not going to be able to change it like this. Fix this line:
C#
string query1 = ("Update Reports set Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where(ID = @id)");
 
Share this answer
 
Comments
Prateek gsharma 7-Jun-18 12:03pm    
if i do so....it is not updating in database.
Nathan Minier 7-Jun-18 12:06pm    
Okay, are you getting an error at this juncture?
Prateek gsharma 7-Jun-18 12:16pm    
no error.but only earlier values are displaying
Nathan Minier 7-Jun-18 12:34pm    
Make sure that the ID parameter is being passed correctly, and properly reflects the ID type in the database.
Prateek gsharma 7-Jun-18 12:58pm    
i passed it correctly but it is not updating any values
The most likely problem is that ID is most likely the Identity column for the table.
If you look at the actual SQL statement itself
SQL
Update    Reports
set       ID = @id
,         Comments = @comments
,         marks = @marks
,         Reviewer = @rvr
,         ReviewerDate = @rvrd
Where    (ID = @id)

You may scratch your head when you think about the logic-- change the ID on the selected row when the ID matches the one provided; Why would you need to change the value to what it already is becomes the question.
The answer is you don't need to; and by not attempting to change it you will not get the exception message you received.

So all you need to do is remove that portion of the SQL Command...
SQL
Update    Reports
set       Comments = @comments
,         marks = @marks
,         Reviewer = @rvr
,         ReviewerDate = @rvrd
Where    (ID = @id)

... and then you should be good to go.
Presuming that ID is the identity column naturally
 
Share this answer
 
Comments
Prateek gsharma 7-Jun-18 12:08pm    
now i have done like above but i unable to update the changes to my row.
Prateek gsharma 7-Jun-18 12:12pm    
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Submit")
{
using (OleDbConnection con1 = new OleDbConnection(con))
{
con1.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con1;
//string query1 = ("insert into Reports (ID,Comments,marks,Reviewer,ReviewerDate) values(@id,@comments,@marks,'" + Environment.UserName + "',Now)");
string query1 = ("Update Reports set Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where (ID = @id)");
cmd.Parameters.AddWithValue("@id", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@comments", dataGridView1.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@marks", dataGridView1.Rows[i].Cells[6].Value);
cmd.Parameters.AddWithValue("@rvr", dataGridView1.Rows[i].Cells[8].Value);
cmd.Parameters.AddWithValue("@rvrd", dataGridView1.Rows[i].Cells[9].Value);

cmd.CommandText = query1;
cmd.ExecuteNonQuery();

}
MessageBox.Show("Comments Submitted", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Prateek gsharma 7-Jun-18 12:14pm    
even though made changes to my query.....values are not updated.

no change in values.this query is not updating any values
MadMyche 7-Jun-18 12:58pm    
Have you tried this directly on SQL?
Prateek gsharma 8-Jun-18 0:53am    
string query1 = ("Update Reports set Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where (ID = @id)");

In the access database
ID data type is AutoNumber
ID field size Long Integer
New values : Increment


But above query is not making any changes to perticular column/Row
is there any other query to make changes in database?

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