Search code examples
c#winformssqldataadaptersqlcommandbuilder

SqlDataAdapter.Update(dataset) method inserts new rows but does not updates existing rows


I am creating winform application that have DataGrigView to present a table. I have a DAL class that is responsible to work with DB.

There are one method that loads data of the table:

 public static void GetItemsByOrder(int orderId, ref DataSet dataSet)
    {
        string queryString = @"Select Id,OrderId as [מס' הזמנה],ItemCode as[מק""ט], ItemName as [שם פריט], ReceiptDate as [ת. הספקה],
                WarrantyExpDate as [באחריות עד],SuppliersItemCode as [מק""ט ספק], Supplier as [ספק], Count as[כמות], Active 
                FROM OrdersManager_Items where OrderId = @param";

        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.AddWithValue("@param", orderId);

        SqlDataAdapter adapter = new SqlDataAdapter(command);


        try
        {
            lock (myLock)
            {
                adapter.Fill(dataSet,"Items");
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLogEntry(LogWriter.LogType.ERROR, string.Format("Failed to get Items by OrderId code from DB."+
                "This is due to exception: {0},\n StackTrace: {1}. ", ex.Message, ex.StackTrace));

            dataSet = null;
        }
    }

And second method that is responsible to update the DB with the changes that were made in the table:

public static bool UpdateItemsByOrder(int orderId, DataSet data)
    {

        string queryString = @"Select Id,OrderId as [מס' הזמנה],ItemCode as[מק""ט], ItemName as [שם פריט], ReceiptDate as [ת. הספקה],
                WarrantyExpDate as [באחריות עד],SuppliersItemCode as [מק""ט ספק], Supplier as [ספק], Count as[כמות], Active 
                FROM OrdersManager_Items where OrderId = @param";


        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.AddWithValue("@param", orderId);

        SqlDataAdapter adapter = new SqlDataAdapter(command);


        try
        {
            lock (myLock)
            {

                SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                int rowsUpdated = adapter.Update(data,"Items");

                return true;
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLogEntry(LogWriter.LogType.ERROR, string.Format("Failed to update Items table in DB. This is due to exception: {0},\n StackTrace: {1}. ", ex.Message, ex.StackTrace));

            return false;
        }
    }

The problem: If in the Items table new rows were aded or deleted - UpdateItemsByOrder add/delete the rows in the DB as expected. But updates in existing rows of the Items table does not updated in DB.

There are no error or exceptions. I have tryed to add builder.GetUpdateCommand() command = no result.

I will be happy to get any help or advice. Thanks

P>S> I am using this MSDN LINK to learn how to work with SQLAdapter


Solution

  • Ok, with the advice of sallushan I got the solution: The reason why DataAdapter doesn't updated the DB is that updated rows in DataTable has RowState value "Unchanged" instead "Modified".

    There is 2 basic ways to resolve this problem:

    1. Update the data direcrly in DataTable and not in DGV
    2. Call DataTable.Rows[indexOfUpdatedRowInDGV].EndEdit() method, after making updates through DGV, as described Here.

    Thanks to all for a help :-)