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....

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

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
Comments
You've got it
agree

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?

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.

This article has been dead for over six months. Start a new discussion instead.