none
Deleting Row from Database C# WPF Linq RRS feed

  • Question

  • I want to delete a row from the Database on a selected row in my Datagrid I already got how to select a row in my Datagrid, but when I delete it on button_click it only deletes it in my Datagrid but not in my actual Database

    I'm using the following code to delete my Rows:

    private void btnDeleteUser_Click(object sender, RoutedEventArgs e)
            {
                DialogResult dr = System.Windows.Forms.MessageBox.Show("Do You Want to Delete this User?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
    
                if (dr == DialogResult.Yes)
                {
                    var query =
                        from t in db.tbl_Users
                        where t.Name == name
                        select t;
    
                    foreach (var t in query)
                    {
                        db.tbl_Users.DeleteOnSubmit(t);
                    }
    
                    db.SubmitChanges();
    
                    var refresh =
                                (from q in db.tbl_Users
                                 select new { Name = q.Name, Rights = q.Rights, Operatortag = q.Operatortag, Active = q.Active, Cardcode = q.CardCode }).ToList();
    
                    dgUsers.ItemsSource = refresh;
                }
            }

    My Database-property is set on Copy If Newer and I already tried to set in to Copy Always but that wouldn't help either.

    Does someone know the answer to my problem?


    Tuesday, March 14, 2017 8:12 AM

All replies

  • Hi programming wolf,

    Based on your description, I create a demo with Localdb, it work well, Could you please provide a bit more information, such as what kind of database you are using, do you encounter any exception?

    If possible, could you please provide a simple demo (include database) which I could reproduce your issue on my side. we'll try my best to find a solution to resolve it.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 16, 2017 9:43 AM
    Moderator