Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have trouble in deleting and updating records in the database using SqlDAtaAdapter, UpdateCommand and DeleteCommand.

Any suggestions are very helpful. Thanks

these are the commands with adapter

C#
class SQLServer
{
    public static SqlDataAdapter daFaculty = new SqlDataAdapter();                
                // FOR UPDATING
                using(SqlCommand cmdUpdate = new SqlCommand())
                {
                    cmdUpdate.Connection = SQLServer.attendanceDB;
                    cmdUpdate.CommandText = "UPDATE tblFaculty SET idno = @idno, lastname = @lastname, firstname = @firstname, middlename = @middlename, fieldSpecs = @fieldspecs WHERE idno = '@idnoref'";
                    cmdUpdate.Parameters.Add("@idno", SqlDbType.VarChar, 0, "idno");
                    cmdUpdate.Parameters.Add("@lastname", SqlDbType.VarChar, 0, "lastname");
                    cmdUpdate.Parameters.Add("@firstname", SqlDbType.VarChar, 0, "firstname");
                    cmdUpdate.Parameters.Add("@middlename", SqlDbType.VarChar, 0, "middlename");
                    cmdUpdate.Parameters.Add("@fieldspecs", SqlDbType.VarChar, 0, "fieldspecs");
                    SqlParameter paramUpdate = cmdUpdate.Parameters.Add("@idnoref", SqlDbType.VarChar, 0, "idno");
                    paramUpdate.SourceVersion = DataRowVersion.Original;
                    daFaculty.UpdateCommand = cmdUpdate;
                }

                // FOR DELETING
                using (SqlCommand cmdDelete = new SqlCommand())
                {
                    cmdDelete.Connection = SQLServer.attendanceDB;
                    cmdDelete.CommandText = "DELETE FROM tblFaculty WHERE idno = '@oldidno'";
                    SqlParameter paramDelete = new SqlParameter();
                    paramDelete = cmdDelete.Parameters.Add("@oldidno", SqlDbType.VarChar, 0, "IDno");
                    paramDelete.SourceVersion = DataRowVersion.Original;
                    daFaculty.DeleteCommand = cmdDelete;
                }
}


These are the function for updating and deleting

C#
public static void DeleteFaculty(string idno)
        {
            General.dtFaculty.Select("idno = '" + idno + "'")[0].Delete();
            SQLServer.daFaculty.Update(General.dtFaculty);
        }

        public static void UpdateFaculty(string idReference, string idno, string lastname, string firstname, string middlename, string fieldspecs)
        {
            DataRow[] row = General.dtFaculty.Select("idno = '" + idReference + "'");

            foreach (DataRow item in row)
            {
                item["lastname"] = lastname;
                item["firstname"] = firstname;
                item["middlename"] = middlename;
                item["fieldspecs"] = fieldspecs;
            }

            SQLServer.daFaculty.Update(General.dtFaculty);
        }


and this is the event for updating and deleting

C#
private void btnSave_Click(object sender, EventArgs e)
{
    General.UpdateFaculty(idsel, txtID.Text, txtLname.Text, txtFname.Text, txtMname.Text, txtFieldSpec.Text);
}

        private void btnDelete_Click(object sender, EventArgs e)
        {
General.DeleteFaculty(idsel);
}
Posted
Updated 2-Mar-15 20:54pm
v3

1 solution

Refer - Concurrency violation: the UpdateCommand affected 0 of the expected 1 records[^].
Quote:

Why:


The UPDATE statement created by the CommandBuilder object uses the values stored in the copy of the DataRow with a DataRowVersion value of Original to identify and update the appropriate row in the database. the CommandBuilder creates a SQL statement that looks for a row which exactly matches all of the original values stored in the DataSet means the value of each and every column in the row is checked. if you are trying to update a row that no longer exists in the database, the update from the DataAdapter will fail with the above exception.
 
Share this answer
 
Comments
Lloydii 3-Mar-15 20:06pm    
I realize that if I change my 'idno' field which is a primary key in the database into an autoincremental 'idno' field, the UpdateCommand and DeleteCommand works. But I want my 'idno' field not incremental because it contains like this "2010-00059-sr-0". How ?
So, that means while it is checking if the row with certain id is present or not, it is not getting anything and thus not updating any row.

I think the where conditions in your query should be like...

".......WHERE idno = @idnoref";

You have extra quotes with the parameter. Can you try like this?
Lloydii 4-Mar-15 4:13am    
Thank you very much sir ! It works now :)
Glad to know that. Please accept the answer and up vote. :)

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