Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# SqlCommandBuilder() Delete is not working

  • 13-02-2013 7:56pm
    #1
    Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭


    Im going insane trying to figure this one out.

    Im populating a DataTable from my database.
    A DataGridView DataSource is set to my DataTable.

    I insert, update and delete rows from the DataTable which in turn updates the DataGridView.

    Im using the below to update my changes back to the database where i pass in the modified DataTable.
    Insert and Update are working but Delete does not remove the deleted rows from the database table.
    private static void updateDatabase(DataTable dt)
    {
        SqlDataAdapter myAdapter = new SqlDataAdapter();
        SqlCommand myCommand = new SqlCommand("SELECT * from dbo.DisplayList", sqlCon);
        SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
    
        myAdapter.SelectCommand = myCommand;
    
        myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand();
        myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
        myAdapter.DeleteCommand = myCommandBuilder.GetDeleteCommand();
    
        myAdapter.Update(dt);
    }
    

    The myAdapter.DeleteCommand.CommandText is:
    DELETE FROM [dbo].[DisplayList] WHERE (([PropertyID] = @p1) AND 
    ([DisplayID] = @p2) AND ([DisplayName] = @p3) AND ((@p4 = 1 AND 
    [TerminalID] IS NULL) OR ([TerminalID] = @p5)))
    

    Anyone have any ideas whats wrong with the above or to help investigation how i can find out what the @p values are being converted to?


Comments

  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    Finally figured it out.

    When deleting the rows from the datatable i was using:
    myDataTable.Rows.RemoveAt(rowindex);
    This completly removes it from the datatable so when the SqlCommandBuilder came around to deleting it from the database it never knew it existed in the first place.

    For it to work you need to flag the row as deleted but not actually delete it.
    myDataTable.Rows[rowindex].Delete();

    By doing this it all worked.


Advertisement