1,105,328 Community Members

Populate combobox from sqldatareader

Member Avatar
Lee21
Light Poster
46 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Attachments user.JPG 45.08KB
Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

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
Member Avatar
Oxiegen
Master Poster
765 posts since Jun 2006
Reputation Points: 88 [?]
Q&As Helped to Solve: 149 [?]
Skill Endorsements: 7 [?]
Featured
 
0
 

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
Member Avatar
Lee21
Light Poster
46 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
verbalurbs
Newbie Poster
15 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
Lee21
Light Poster
46 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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!

Member Avatar
verbalurbs
Newbie Poster
15 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

@verbalurbs

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

rules.

Member Avatar
verbalurbs
Newbie Poster
15 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@verbalurbs

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

rules.

sorz

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article