简体   繁体   中英

VB.NET Save the delete command in access database in datagridview

I have working code that uses a SQL command to delete a select row from a database and that command works if I use it in access. However, when I want to do this with my delete button in VB.NET it does not save it. When I check in program if it is gone it works and updates everything and so forth. When I exit the program and start it again it goes back to normal. I need help with the fix badly !

Imports System.Data.OleDb

Public Class CustomersForm
    Dim Contact(-1) As CustomerData
    ''' <summary>
    ''' Load Form and Fill Table Adapter
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub CustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MooselyAdventuresDataSet.tblCustomers' table. You can move, or remove it, as needed.
        Me.TblCustomersTableAdapter.Fill(Me.MooselyAdventuresDataSet.tblCustomers)
        LoadCustomerInformation()
    End Sub
    ''' <summary>
    ''' Structure for data for datagrid view and to hold data for cells
    ''' </summary>
    ''' <remarks></remarks>
    Structure CustomerData
        Public LongCustomerID As Long
        Public FirstName As String
        Public LastName As String
        Public Address As String
        Public City As String
        Public State As String
        Public Zip As Integer
        Public Email As String
    End Structure
    ''' <summary>
    ''' Close Customers Form
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btCustClose_Click(sender As Object, e As EventArgs) Handles btCustClose.Click
        Me.Close()
    End Sub
    ''' <summary>
    ''' Call the Sql statement and retrieve data and fill the datagridview control
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadCustomerInformation()
        dgvCustomers.Rows.Clear()
        dgvCustomers.Columns.Clear()
        Dim CustData As New CustomerInfoGetter

        'Set customer string to the module
        CustData.ConnectionString = mdlConnectString.ConnectionString
        CustData.Sql = "SELECT CustomerLast, CustomerFirst, CustomerAddress, CustomerCity, CustomerState, CustomerZip, CustomerEmail, CustomerID FROM tblCustomers"
        'Add Columns for datagrid view
        dgvCustomers.Columns.Add("First Name", "First Name")
        dgvCustomers.Columns.Add("Last Name", "Last Name")
        dgvCustomers.Columns.Add("Street Address", "Street Address")
        dgvCustomers.Columns.Add("City", "City")
        dgvCustomers.Columns.Add("State", "State")
        dgvCustomers.Columns.Add("Zip", "Zip")
        dgvCustomers.Columns.Add("Email", "Email")
        dgvCustomers.Columns.Add("CustomerID", "CustomerID")



        For I As Integer = 0 To CustData.DS.Tables(0).Rows.Count - 1
            ReDim Preserve Contact(Contact.Length)
            With Contact(Contact.Length - 1) 'loop to add data to datagridview and getting data from sql statement'
                .FirstName = CustData.DS.Tables(0).Rows(I).Item("CustomerFirst").ToString()
                .LastName = CustData.DS.Tables(0).Rows(I).Item("CustomerLast").ToString()
                .Address = CustData.DS.Tables(0).Rows(I).Item("CustomerAddress").ToString()
                .City = CustData.DS.Tables(0).Rows(I).Item("CustomerCity").ToString()
                .State = CustData.DS.Tables(0).Rows(I).Item("CustomerState").ToString()
                .Zip = CustData.DS.Tables(0).Rows(I).Item("CustomerZip").ToString()
                .Email = CustData.DS.Tables(0).Rows(I).Item("CustomerEmail").ToString()
                .LongCustomerID = CustData.DS.Tables(0).Rows(I).Item("CustomerID").ToString()
                dgvCustomers.Rows.Add() 'add a row to fill information'
                dgvCustomers.Rows(I).Cells(0).Value = .FirstName
                dgvCustomers.Rows(I).Cells(1).Value = .LastName
                dgvCustomers.Rows(I).Cells(2).Value = .Address
                dgvCustomers.Rows(I).Cells(3).Value = .City
                dgvCustomers.Rows(I).Cells(4).Value = .State
                dgvCustomers.Rows(I).Cells(5).Value = .Zip
                dgvCustomers.Rows(I).Cells(6).Value = .Email
                dgvCustomers.Rows(I).Cells(7).Value = .LongCustomerID


            End With
        Next

        dgvCustomers.Columns(7).Visible = False 'Make Last Row Invisible'
    End Sub
    ''' <summary>
    ''' When a cell is clicked (Actually entire rows are only selected....) it will changed the textboxes and combo box to according
    ''' values
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgvCustomers_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvCustomers.CellClick
        Dim i As Integer = dgvCustomers.CurrentRow.Index

        'Fill Text Boxes and Such When Clicked'
        tbLastName.Text = dgvCustomers.Rows(i).Cells(1).Value
        tbFirstName.Text = dgvCustomers.Rows(i).Cells(0).Value
        tbStreet.Text = dgvCustomers.Rows(i).Cells(2).Value
        tbCity.Text = dgvCustomers.Rows(i).Cells(3).Value
        cbStates.Text = dgvCustomers.Rows(i).Cells(4).Value
        tbZipCode.Text = dgvCustomers.Rows(i).Cells(5).Value
        tbEmail.Text = dgvCustomers.Rows(i).Cells(6).Value

    End Sub

    Private Sub btDelete_Click(sender As Object, e As EventArgs) Handles btDelete.Click

        Try
            Dim CustData As New CustomerInfoGetter
            Dim CustIDDelete As String


            CustIDDelete = dgvCustomers.Rows(dgvCustomers.CurrentRow.Index).Cells(7).Value

            'Set customer string to the module
            CustData.ConnectionString = mdlConnectString.ConnectionString
            CustData.Sql = "DELETE FROM tblCustomers WHERE CustomerID = " & CustIDDelete
            LoadCustomerInformation()


        Catch
            MessageBox.Show("You Can't Delete When Their Are No Records !", "Error !")
        End Try

    End Sub

    Private Sub btAdd_Click(sender As Object, e As EventArgs) Handles btAdd.Click

    End Sub
End Class

I am not sure what you're doing exactly in your code but I have written a little function to do what you want

Public Shared Function CreateCustomerAdapter( _
    connection As OleDbConnection) As OleDbDataAdapter 

    Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter()
    Dim command As OleDbCommand
    Dim parameter As OleDbParameter

    ' Your DELETE command.
    command = New OleDbCommand( _
        "DELETE * FROM tblCustomers WHERE CustomerID = ?", _
        connection)

    parameter = command.Parameters.Add( _
        "CustomerID", OleDbType.Char, 5, "CustomerIDDelete")

    parameter.SourceVersion = DataRowVersion.Original

    dataAdapter.DeleteCommand = command

    Return dataAdapter
End Function

and then

command.ExecuteNonQuery()

Last but not least, remember to use Parametrization on your SQL sentence since it will follow the good practice and prevent security issues such as SQL injection. That's what OleDbParameter is for. Check the MSDN page below for more info

OleDbCommand.Parameters Property

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