简体   繁体   中英

How to delete the data of a row in the excel sheet DB from datagridview through a Delete button “BtnDelete”

I don't know how to delete the data of a row in the excel sheet DB from DataGridView. its not showing any error but the selected row in DataGridView aslo not getting delete. No idea what's going wrong in my code

Here is my code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TxtExamtime.Format = DateTimePickerFormat.Custom
        TxtExamtime.CustomFormat = "hh:MM tt"
        cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;"
        cn.Open()
        FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]")

End Sub

Private Sub FillDataGridView(ByVal Query As String)
    da = New OleDbDataAdapter(Query, cn)
    dt.Clear()
    da.Fill(dt)
    With DataGridView1
        .DataSource = dt
        .Columns(0).HeaderText = "ID"
        .Columns(1).HeaderText = "Family Name"
        .Columns(2).HeaderText = "Given Name"
        .Columns(3).HeaderText = "Gender"
        .Columns(4).HeaderText = "DOB"
        .Columns(5).HeaderText = "Exam Date"
        .Columns(6).HeaderText = "Exam Time"
        .Columns(7).HeaderText = "Street Name"
        .Columns(8).HeaderText = "House Nr"
        .Columns(9).HeaderText = "PLZ"
        .Columns(10).HeaderText = "City"
        .Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    End With
End Sub

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
    Try
        With cm
            .Connection = cn
            .CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )"
            .ExecuteNonQueryAsync()
        End With
        FillDataGridView("select * from [edit$]")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
        Return
    End Try
    MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
End Sub

Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
    Try
        With cm
            .Connection = cn
            .CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
            .ExecuteNonQueryAsync()
        End With
        FillDataGridView("select * from [edit$]")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Information, Text)
        Return
    End Try
    MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub

Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click


Try
            With cm
                .Connection = cn
                .CommandText = "Delete [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                .ExecuteNonQueryAsync()
            End With
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, Text)
            Return
        End Try
        MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
    End Sub

Your DELETE statement is wrong. It should be throw an exception (a MsgBox showing the error as per your code) when trying to delete the record.

The correct syntax is

DELETE FROM table_name
WHERE some_column=some_value;

Eg:

DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';

Supposing that you want to delete the record that matches with ALL the textboxes (as per your code), your CommandText should be something like this

.CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and id ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"

Edit

Also your message Succesfully Deleted! is outside of the Catch . It will show that message even when a exception has been thrown

Try this

Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click

Try
            With cm
                .Connection = cn
                .CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and id ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                .ExecuteNonQuery()                    
            End With
            MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, Text)

End Try

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM