hi...... projot here

I am facing a problem in the code
i have to populate the combo box from datagrid view from database
just help me in doing it this code is giving me an error
my contact no is 9049776416

Imports System.Data
Imports MySql.Data.MySqlClient

Public Class supplier_update
Dim con1 As MySqlConnection
Dim con2 As MySqlConnection
Dim con3 As MySqlConnection
Dim con4 As MySqlConnection
Dim cm As MySqlCommand
Dim cmm As MySqlCommand
Dim cmmm As MySqlCommand

Dim adpt As New MySqlDataAdapter
Dim ds As New DataSet
Dim a As Integer
Dim j As Integer = 0
Dim ca As String = ""
Dim cq As String = ""
Dim billno As Integer
Dim r_name As String
Dim d, t As Integer
Dim h As Integer = 0
Dim cs As String = "cash"
Dim i As Integer = -1
Dim va As Integer = 0



Private Sub supplier_update_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    Try
        con1 = New MySqlConnection("database=liberty_datbase;data source=localhost;user id=root;password=82684")
        con2 = New MySqlConnection("database=liberty_datbase;data source=localhost;user id=root;password=82684")
        con3 = New MySqlConnection("database=liberty_datbase;data source=localhost;user id=root;password=82684")
        con4 = New MySqlConnection("database=liberty_datbase;data source=localhost;user id=root;password=82684")


        loadData()
        cal1()



    Catch ex As Exception
        MessageBox.Show("unable to connect to the database" & ex.ToString(), "supplier", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)


    End Try

End Sub
Sub cal1()

    con2.Open()
    Dim dr As MySqlDataReader
    categoryDataGridViewComboBoxColumn.Items.Clear()
    cm = New MySqlCommand("select distinct(category) from inventory", con2)

    cm.CommandType = CommandType.Text

    dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

    If dr.HasRows Then
        While dr.Read
            'If Not IsDBNull(dr.Item("category")) Then
            categoryDataGridViewComboBoxColumn.Items.Add(dr.Item("category"))
            ' End If
        End While
    End If

    dr.Close()



End Sub



Sub c()
    Try


    cal1()

    If sup_gv.Rows(j).Cells(0).Selected = True Then
            sup_gv.Rows(j).Cells(0).ReadOnly = True

        con3.Open()
        Dim dr1 As MySqlDataReader
        item_name.Items.Clear()
        cm = New MySqlCommand("select item_name from inventory where category='" & sup_gv.Rows(j).Cells(0).Value() & "'", con3)
        MessageBox.Show("select item_name from inventory where category='" & sup_gv.Rows(j).Cells(0).Value() & "'")
        cm.CommandType = CommandType.Text
        dr1 = cm.ExecuteReader(CommandBehavior.CloseConnection)

            If dr1.HasRows Then
                While dr1.Read
                    If Not IsDBNull(dr1.Item("item_name")) Then
                        item_name.Items.Add(dr1.Item("item_name"))
                    End If
                End While

            End If

            va = va + 1
            j = j + 1

            dr1.Close()

        Else
            MessageBox.Show("errrorrrrr")
        End If
    Catch ex As Exception
        MessageBox.Show("error")

    End Try
End Sub


Sub loadData()
    con1.Open()
    Try

        adpt = New MySqlDataAdapter()
        cm = New MySqlCommand()
        ds = New DataSet()
        cm.CommandText = "select * from supplier"
        cm.Connection = con1
        adpt.SelectCommand = cm
        adpt.Fill(ds, "supplier")


    Catch ex As Exception
        MessageBox.Show("unable to load data" & ex.ToString(), "supplier", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)

    End Try
    con1.Close()

End Sub


Private Sub sup_gv_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles sup_gv.CellLeave
    Dim retailer_price, r, cons As Integer
    Dim total As Integer = 0


    For va As Integer = 0 To sup_gv.RowCount - 2
        MessageBox.Show("val" & va)

        Dim rate, qty, cal As Integer

        rate = sup_gv.Rows(va).Cells(3).Value()
        qty = sup_gv.Rows(va).Cells(2).Value()
        cons = sup_gv.Rows(va).Cells(4).Value()
        cal = rate * qty
        sup_gv.Rows(va).Cells(6).Value() = cal


        r = (cons - rate) * 0.5
        retailer_price = r + rate

        sup_gv.Rows(va).Cells(5).Value() = retailer_price



        total = total + sup_gv.Rows(va).Cells(6).Value()
        gt.Text = total



    Next va


    c()



End Sub





Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    Dim a = RadioButton1.Checked


    If (a = False) Then

        cheque_txt.Visible = False
        Label5.Visible = False

    Else
        cheque_txt.Visible = True
        Label5.Visible = True

    End If
End Sub




Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles back.Click
    Me.Hide()
    mainmenu.Show()
End Sub

Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click
    name_txt.Text = ""
    RadioButton1.Checked = False
    RadioButton2.Checked = True
    cheque_txt.Text = ""
    sup_gv.Rows.Clear()
    gt.Text = ""
End Sub

Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit.Click
    con4.Open()
    Try

        If name_txt.Text = "" Then
            MsgBox("Enter the name", MsgBoxStyle.Information, "verify")

        ElseIf cheque_txt.Text = "" Then
            MsgBox("Enter the cheque no", MsgBoxStyle.Information, "verify")



        Else


            Dim sst As String

            sst = "select max(bill_no) from supplier"
            ds = New DataSet()
            adpt = New MySqlDataAdapter(sst, con4)
            adpt.Fill(ds)


            If IsDBNull(ds.Tables(0).Rows(0).Item(0)) Then
                d = 1
            Else

                d = ds.Tables(0).Rows(0).Item(0) + 1
            End If
            bill_no.Text = d


            r_name = name_txt.Text.Trim


            For i As Integer = 0 To sup_gv.RowCount - 2

                Dim st As String
                st = "select max(trans_no) from supplier"
                ds = New DataSet()
                adpt = New MySqlDataAdapter(st, con4)
                adpt.Fill(ds)


                If IsDBNull(ds.Tables(0).Rows(0).Item(0)) Then
                    t = 1
                Else

                    t = ds.Tables(0).Rows(0).Item(0) + 1
                End If

                MessageBox.Show("insert into supplier values(" & t & "," & d & ",'" & r_name & "','" & sup_gv.Rows(i).Cells(0).Value() & "','" & sup_gv.Rows(i).Cells(1).Value() & "'," & sup_gv.Rows(i).Cells(2).Value() & "," & sup_gv.Rows(i).Cells(3).Value() & "," & sup_gv.Rows(i).Cells(4).Value() & "," & sup_gv.Rows(i).Cells(5).Value() & "," & sup_gv.Rows(i).Cells(6).Value() & "," & gt.Text & ",'" & cheque_txt.Text & "','" & txt_date.Text & "')")

                cm.CommandText = "insert into supplier values(" & t & "," & d & ",'" & r_name & "','" & sup_gv.Rows(i).Cells(0).Value() & "','" & sup_gv.Rows(i).Cells(1).Value() & "'," & sup_gv.Rows(i).Cells(2).Value() & "," & sup_gv.Rows(i).Cells(3).Value() & "," & sup_gv.Rows(i).Cells(4).Value() & "," & sup_gv.Rows(i).Cells(5).Value() & "," & sup_gv.Rows(i).Cells(6).Value() & "," & gt.Text & ",'" & cs & "','" & cheque_txt.Text & "','" & txt_date.Text & "')"category.Selected() & "','" & item_name.Selected() & "'," & sup_gv.Rows(i).Cells(2).Value() & "," & sup_gv.Rows(i).Cells(3).Value() & "," & sup_gv.Rows(i).Cells(4).Value() & "," & sup_gv.Rows(i).Cells(5).Value() & "," & gt.Text & ",'" & cheque_txt.Text & "','" & txt_date.Text & "')"

                cm.Connection = con4
                cm.ExecuteNonQuery()




            Next i

            MsgBox("data is stored")
            loadData()

        End If
        con4.Close()



    Catch ex As Exception
        MsgBox("Error:Inserting data")



    End Try







End Sub




Private Sub sup_gv_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles sup_gv.RowLeave
    va = va + 1

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each row As DataGridViewRow In sup_gv.SelectedRows
        sup_gv.Rows.Remove(row)
    Next
End Sub

End Class

Recommended Answers

All 4 Replies

why you have 4 connection with same string?

because it was giving error with some connection ,do u have any other opinion .......

my exact problem is
i want to populate combo box in data grid view from database
i have a data gride view which contain "catogory , item_name , qty , rate ,consumer_price,total"
this is my data grid view

when u run the program
1st it has to populate the combo box in datagrid view from inventory(inventory is the name of the database)
then the user selects the category
one's the category is selected the item name of that particulor brand has to be populated in 2nd combo box.

eg:: category (" brandy , Whisky , Vodka ,etc")
when brandy is selected all the brandy items has to be populated in the item name combo box.......

plz help me

or contact me........plzzzzz
do replay

ok here is an example :

(1). i have 2 tables in database, name was "superhero" and "badguy"

(2). both tables have items :

"superhero's" items is :
a. Superman
b. Batman
c. Optimus

"badguy's" items is :
a. Lex Luthor
b. Joker
c. Megatron

i want that what ever user select in first column (Superhero or Badguy), it will give me its member (Superman, Batman, Optimus or Lex Luthor, Joker, Megatron) in second dolumn.

(3). in my form, i have a DGV (DataGridView), i add 2 columns in there (both type is : ComboBoxCell)

(4). for first column's collection, i edit its item through its properties, i add :

"Superhero"
"Badguy"

(5). in event CurrentCellChanged for DGV, i put this code :

     Private Sub DataGridView1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged

       Try

            Using conn As New MySqlConnection("server=localhost;user=root;password=your_password;database=your_database_name")

                Dim comboCell As New DataGridViewComboBoxCell
                comboCell = DataGridView1.CurrentRow.Cells(1)

                If DataGridView1.CurrentRow.Cells(0).Value = "Superhero" Then
                    conn.Open()
                    Dim command As New MySqlCommand("SELECT * FROM superhero;", conn)
                    Dim reader As MySqlDataReader = command.ExecuteReader
                    comboCell.Items.Clear()
                    While reader.Read()
                        comboCell.Items.Add(reader("superhero_name"))
                        comboCell.ValueMember = reader("superhero_name")
                    End While
                    reader.Close()
                    command.Dispose()
                    conn.Close()
                Else
                    conn.Open()
                    Dim command As New MySqlCommand("SELECT * FROM badguy;", conn)
                    Dim reader As MySqlDataReader = command.ExecuteReader
                    comboCell.Items.Clear()
                    While reader.Read()
                        comboCell.Items.Add(reader("badguy_name"))
                        comboCell.ValueMember = reader("badguy_name")
                    End While
                    reader.Close()
                    command.Dispose()
                    conn.Close()
                End If

            End Using

        Catch Ex As Exception
           MsgBox(Ex.Message)        
        End Try 

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.