Updating Database from DataGridView with combo box not working in vb.net

97 Views Asked by At

Updating Database from DataGridView with combo box not working in vb.net I want to update by doing a combobox filter for the database column "INVNO"

please best solution. another one in the code below and do I need to use using-statement?

Thanks

Friend Class DatabaseManager
    Private connectionString As String = ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString
    Private conn As OleDbConnection
    Private adapter As OleDbDataAdapter
    Private builder As OleDbCommandBuilder
    Private threadHelper As Thread
Public Sub New()
        conn = New OleDbConnection(connectionString)
    End Sub

    Public Function FillData(ByVal command As String) As DataTable
            If command.Trim().Length < 0 Then
                Throw New Exception("Command text is empty")
            End If
            Dim _ad As New OleDbDataAdapter(command, conn)
        Dim dt As New DataTable()
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
        _ad.Fill(dt)
        adapter = _ad
        If threadHelper IsNot Nothing AndAlso threadHelper.IsAlive Then
            threadHelper.Abort()
        End If
        threadHelper = New Thread(New ThreadStart(AddressOf Do_Work))
        threadHelper.Start()

        Return dt
    End Function

    Private Sub Do_Work()
        builder = New OleDbCommandBuilder(adapter)
        adapter.InsertCommand = builder.GetInsertCommand()
        adapter.UpdateCommand = builder.GetUpdateCommand()
        adapter.DeleteCommand = builder.GetDeleteCommand()
    End Sub
    Public Function UpdateData(ByVal dt As DataTable) As Integer
        If adapter Is Nothing Then
            Throw New Exception("Parameter adapter is null")
        End If
        Return adapter.Update(dt)
    End Function
    End Class
 Private Sub LoadData()
            Dim sql As String = String.Empty
            If ComboBox1.SelectedIndex > -1 Then
''Parameters' is not a member of 'String'
                Command.Parameters.AddWithValue("@INVNO", CStr(ComboBox1.SelectedValue))
                dtData = db.FillData("SELECT * FROM DEMOTABLEPRODUCT WHERE INVNO = @INVNO")
                gvMain.DataSource = dtData
            End If
End Sub

Private Sub UpdateData()
            Try
                Dim i As Integer = db.UpdateData(dtData)
                MessageBox.Show(i & " : records updated successfully")
            Catch err As Exception
                MessageBox.Show(err.Message)
            End Try
        End Sub

        Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
            Me.LoadData()
        End Sub

I was expecting the combobox to do filters

0

There are 0 best solutions below