hi to all im new here, i have some difficulty in displaying records from database to my combobox here is my code in the form_load

Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As New SqlConnection
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\lito\Documents\QMP_DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        con.Open()
        Dim da As New SqlDataAdapter("select * from Subject_Info", con)
        Dim ds As New DataSet
        da.Fill(ds, "subjectInfo")
        Dim dt As DataTable
        dt = ds.Tables("Subject_Info")
        Dim dr As DataRow
        Dim i As Integer
        For i = 0 To dt.Rows.Count
            dr = dt.Rows(i)
            cbSubj.Items.Add(dr(1).ToString)
        Next
end sub

can anyone please help...this is my project....

Recommended Answers

All 7 Replies

It would be better to use datasource property of comboBox to pass data from table to comboBox:

Private Sub Form5_Load(sender As System.Object, e As System.EventArgs)
	Dim con As New SqlConnection()
	con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\lito\Documents\QMP_DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
	con.Open()
	Dim da As New SqlDataAdapter("select * from Subject_Info", con)
	Dim table As New DataTable()
	da.Fill(table)
	comboBox1.DataSource = New BindingSource(table, Nothing)
	comboBox1.DisplayMember = "Name"
	'write the column name which will be diplayed
	'you can even use  valueMember property, 
	'Names - DisplayMember - this is was you see in comboBox
	'IDs - ValueMember can be used as additional value of Person
	comboBox1.ValueMember = "ID"
	'column name for value         
End Sub
commented: agree +13
commented: You've got it +6

hi to all im new here, i have some difficulty in displaying records from database to my combobox here is my code in the form_load

Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As New SqlConnection
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\lito\Documents\QMP_DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        con.Open()
        Dim da As New SqlDataAdapter("select * from Subject_Info", con)
        Dim ds As New DataSet
        da.Fill(ds, "subjectInfo")
        Dim dt As DataTable
        dt = ds.Tables("Subject_Info")
        Dim dr As DataRow
        Dim i As Integer
        For i = 0 To dt.Rows.Count
            dr = dt.Rows(i)
            cbSubj.Items.Add(dr(1).ToString)
        Next
end sub

can anyone please help...this is my project....

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

	 Try
		'declare variables

		Dim con As New SqlConnection
		Dim objDataAdapter As New SqlDataAdapter()
		Dim objDataSet As New DataSet()

		'set database connection 
		con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\lito\Documents\QMP_DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"


            '//state dataset of combo box
            ' Set the SelectCommand properties...
            objDataAdapter.SelectCommand = New SqlCommand()
            objDataAdapter.SelectCommand.Connection = con
            objDataAdapter.SelectCommand.CommandText = "select * from Subject_Info"
            objDataAdapter.SelectCommand.CommandType = CommandType.Text

            ' Open the database connection...
            con.Open()
            ' Fill the DataSet object with data...
            objDataAdapter.Fill(objDataSet, "subject_info")
            ' Close the database connection...
	   con.Close()

            With originComboBox
                .DataSource = objDataSet
                .DisplayMember = "subject_info.subjectinfo"
            End With

        Catch ex As Exception

        End Try

mitja, i use your code when i debug it shows no error, but my combobox displays nothing...is there something wrong with my database?

:-( please somebody help me....tomorow is the deadline of this program....

i user your code it does run but it returns null in my combobox

Use a debugger. Put a break point to see if dataTable gets filled up!
My guess is that it does not. So there is something or with sql query (query statement, so it does no select from correct table (or this one is empty)), or maybe with conn.String (but if it would be it, there will be an exception thrown out.

Best is to go through the code line by line, and check dataTable has any values. This is the most I can say right now.

        ComboBox1.DataSource = ds.Tables(0)
        ComboBox1.ValueMember = "id"
        ComboBox1.DisplayMember = "lname"

full source Click Here

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.