I want to retrive data from table to combobox I used this coding though I didn't get an error the outpiut(data) is not displaing in the combobox

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MessageBox.Show("Please Insert the Sample note no", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Try
            myConnection = New SqlConnection(connectionstring)
            myConnection.Open()


            ''''''''''''
            myCommand1 = New SqlCommand(" SELECT [User_Id] ,[P_Id],[Quantity],[Category]  ,[S_Name]FROM [VBP].[dbo].[Purchase_Order] Where [O_N0]='" & Val(TextBox3.Text) & "'", myConnection)
            Dim dr As SqlDataReader = myCommand1.ExecuteReader
            While dr.Read()
                ' Insert into Product_Details (P_Id,Category,Quantity,Date,Time,Name,P_Purchased_Price,P_selling_Price,No_Of_Units_Purchased_last,Profit_Margin,Discount,Discounted_Price) values ('" & pid & "','" & pc1 & "','" & quantity & "','" & date1 & "','" & TextBox1.Text & "','" & Name & "','" & pp & "','" & sp & "','" & TextBox3.Text & "','" & profit.Text & "','" & TextBox7.Text & "','" & TextBox2.Text & "

                TextBox1.Text = (dr(0))
                '   ComboBox2.Text = Val(dr(1)).ToString
                TextBox4.Text = Val(dr(1))
                'ComboBox3.Text = Val(dr(5)).ToStrin3
                ' ComboBox1.Text = Val(dr(3)).ToString
                ' ComboBox3.Text = Val(dr(4)).ToString

                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                'currentbalance = Double.Parse(dr(2).ToString)

            End While

 Dim ds As DataSet = New DataSet
                ComboBox3.DataSource = ds.Tables("Purchase_Order")
                ComboBox3.DisplayMember = "S_Name"


                ds = Nothing

        Catch err As Exception
            MsgBox(err.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

Recommended Answers

All 8 Replies

First thing to do is debug your code and make sure there are values to insert into the combo box after the selection statement has run e.g. check the reader isn't empty.

You are declaring a dataset, initialize it and then proceed to use it as a datasource without ever filling it with data.
It shouldn't populate your combobox as it holds no data.

I changed the coding like this but still i didn't get errors nor a output in the combobox

        Try
            myConnection = New SqlConnection(connectionstring)
            myConnection.Open()
            sqlcmd = New SqlCommand(" SELECT [S_Name]FROM [VBP].[dbo].[Purchase_Order] Where [O_N0]='" & Val(TextBox3.Text) & "'", myConnection)

            Dim da As SqlDataReader
            'ds.Fill(da, "table1")
            da = sqlcmd.ExecuteReader
            While da.Read
                ComboBox3.Items.Add(da("S_Name"))
            End While
        Catch err As Exception
            MsgBox(err.Message, MsgBoxStyle.Exclamation)
        End Try
        myConnection.Close()

Read this post on how to retrieve data in a combobox: http://www.daniweb.com/software-development/vbnet/threads/446843/multicolumn-combobox-from-access-data#post1928418

Although it's about separating what the user sees from what the db or the program will read, you can go ahead and modify it to suit your needs.

Alternatively you can use a bindingsource:

        Try
            myConnection = New SqlConnection(connectionstring)
            myConnection.Open()
            sqlcmd = New SqlCommand(" SELECT [S_Name]FROM [VBP].[dbo].[Purchase_Order] Where [O_N0]='" & Val(TextBox3.Text) & "'", myConnection)

            Dim bind as new bindingsource
            bind = sqlcmd.ExecuteReader
            ComboBox3.datasource = bind 

        Catch err As Exception
            MsgBox(err.Message, MsgBoxStyle.Exclamation)
        End Try             
        myConnection.Close()

Try this

Try
    Dim com As New SqlCommand
    Dim adap As New SqlDataAdapter
    Dim table As New DataTable
    Dim str As String

        str = "SELECT [S_Name]FROM [VBP].[dbo].[Purchase_Order] Where [O_N0]='" & Val(TextBox3.Text) & "'", myConnection"

    com.Connection = Con
    com.CommandText = str
    table.SelectCommand = cmd
    adap.Fill(table)

    If table.Rows.Count > 0 Then

    combobox.DataSource = table
    combobox.DisplayMember = "S_Name"
    combobox.ValueMember = "S_Name"

    end if

Catch ex As Exception
            MsgBox("An error occured:  '" & vbCrLf & ex.Message & "'", vbOKOnly + vbInformation, "Error")
End Try

try this

dim con as new sqlconnection("connection string")
con.open()
dim da as new sqldataadapter("select userId , userName from users",con)
dim dt as new datatable

da.fill(dt)
comboBox.datasource= dt;
with combobox
    .displaymember="userName"
    .valuemember="userId"
end with

con.close()

Hope this will help you

thank you.

please help me i want to add all field ['fullname'] from mysql server to combobox of vb6 ..

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.