Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

May I ask after insert data to combo box and save it to the database at the same time delete data from combo box can it make it.

$
0
0

I develop a program one way is insert data from combo box another one is delete data from combo box but i face error message from delete data from combo box below is my code. may i ask is possible at the same time can insert data from combo box after delete it from combo box. can guide me thanks 

 If cnn.State = ConnectionState.Open Then
            cnn.Close()
        End If

        Dim da As New OleDbDataAdapter
        Dim dt As New DataTable
        cnn.Open()

        Try
            Dim coddelete As New OleDbCommand
            coddelete.Connection = cnn
            coddelete.CommandText = "DELETE FROM tbl_Category WHERE Category = @Category;"
            coddelete.Parameters.AddWithValue("@Category", ComboBox1.SelectedItem)

            coddelete.ExecuteNonQuery()
            MsgBox("Delete data successful", MsgBoxStyle.Information, "Message")

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

        da.SelectCommand = New OleDbCommand("select * from tbl_Category", cnn)
        da.Fill(dt)
        ComboBox1.DataSource = (dt)
        ComboBox1.DisplayMember = "Category"
        Me.Button2.PerformClick()
        cnn.Close()

insert data to combo box code

If cnn.State = ConnectionState.Open Then
            cnn.Close()
        End If

        Dim da As New OleDbDataAdapter
        Dim dt As New DataTable
        cnn.Open()

        Try
            Dim codinsert As New OleDbCommand
            codinsert.Connection = cnn
            codinsert.CommandText = "INSERT INTO tbl_Category ([Category])" &
        "VALUES('" & Me.ComboBox1.Text & "')"

            'Trim'
            If ComboBox1.Text.Trim = "" Then
                MessageBox.Show("Please Insert Text", "Error Message")
                Exit Sub
            End If
            'Trim'
            codinsert.ExecuteNonQuery()
            MsgBox("Save data successful", MsgBoxStyle.Information, "Message")

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

        da.SelectCommand = New OleDbCommand("select * from tbl_Category", cnn)
        da.Fill(dt)
        ComboBox1.DataSource = (dt)
        ComboBox1.DisplayMember = "Category"
        Me.Button2.PerformClick()
        cnn.Close()
    End Sub

thanks a lot


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>