guys I need to populate all the values of a specific column in each rows of my table from my database. The specific column that i need is the "position",I'm using sqldatareader here's my code. I know that I need to use looping statements and the items.add properties of the combobox but I dont know how will I do it..hope you help me here..

Private Sub cboPosition_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPosition.Click


        scmd = New SqlCommand("SELECT KPIPOSTCODE FROM KKPIPOSITION", sqlcon)

        sqlcon.Open()
        scmd.CommandType = CommandType.Text
        dr = scmd.ExecuteReader

        dr.Read()
        cboPosition.Items.Add

Recommended Answers

All 8 Replies

You can use DataBind approach to populate the combo.

Dim Adp as new SqlDataAdapter("SELECT  KPIPOSTCODE FROM KKPIPOSITION", sqlcon)

Dim Dt as New DataTable

Adp.Fill(Dt)

cboPosition.DataSource=Dt
cboPosition.DisplayMember=Dt.Columns(0).ColumnName

Or if you prefer the datareader, here's how it's done.

Private Sub cboPosition_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPosition.Click
        scmd = New SqlCommand("SELECT KPIPOSTCODE FROM KKPIPOSITION", sqlcon)

        sqlcon.Open()
        scmd.CommandType = CommandType.Text
        dr = scmd.ExecuteReader(CommandBehavior.CloseConnection)

        If dr.HasRows
                While dr.Read
                        If Not IsDBNull(dr.Item("KPIPOSTCODE")) Then
                                cboPosition.Items.Add(dr.Item("KPIPOSTCODE"))
                        End If
                End While
        End If
        dr.Close()
End Sub

Guys it was really helpful.. Thanx a lot! ^_^

Hello
I have a similar issue where I am trying to populate a textbox with a primary key value by selecting a name from a combobox which is stored in sql server.
i have tried different methods and nothing seem to work .
can you guide on this please..

this is the code for combobox. i placed it in the formload

Public Sub RtnCmbVal()
        Dim conn As SqlConnection
        Dim str As String = "select ConsultantId, FullName from Derma_Consultants"
        Dim da As SqlDataReader
        conn = GetConnection()
        Dim ds As New SqlDataAdapter(str, conn)
        Dim cmdGet As New SqlCommand(str, conn)
        conn.Open()
        da = cmdGet.ExecuteReader
        While da.Read
            consultantcmb.Items.Add(da("FullName"))
        End While
    End Sub
Dim Adp as new SqlDataAdapter("SELECT  KPIPOSTCODE FROM KKPIPOSITION", sqlcon) Dim Dt as New DataTable Adp.Fill(Dt) cboPosition.DataSource=DtcboPosition.DisplayMember=Dt.Columns(0).ColumnNameDim Adp as new SqlDataAdapter("SELECT  KPIPOSTCODE FROM KKPIPOSITION", sqlcon)

Dim Dt as New DataTable

Adp.Fill(Dt)

cboPosition.DataSource=Dt
cboPosition.DisplayMember=Dt.Columns(0).ColumnName

I'm using this code and I think this a lot easier.

try using where statements to filter your data from the database, for example..

"SELECT ConsultantId, FullName from Derma_Consultants WHERE idnum = '" & combobox.text & "',sqlcon"

try to insert this code to the click event of the combobox.. then bind to the textbox the data from the datatable..I know this is not the exact code that you need but I'm only giving you the idea. Hope this would help you!

Hi Lee
Thanks for your reply I have tried using your method before but never added any code to the click event of the combo box. and I dont think Im sure as to what to add. the options I am getting are the click events for the buttons on the form and when i use that it doesnt give me the result I want.
I am wondering if its because I am selecting fullname first and not the primary key.

@verbalurbs

Do not hijack another thread to ask your question but start your own thread instead.

rules.

@verbalurbs

Do not hijack another thread to ask your question but start your own thread instead.

rules.

sorz

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.