Well something is missing of me (again :P) and I need some help.. I am trying
to get files for my database and load it into comboBox. But for some reason I get systax
error.. As I mention into other post I am not familliar with VB so please check my code
and let me know where I mess up... Here is my code:::

Public Class Form2

 Dim DatabaseOpenConnection As New SqlConnection("Server=localhost;User Id=root;Password=root;Database=test")

Dim da As SqlDataAdapter("SELECT * FROM products", DatabaseOpenConnection)

'' I get syntax error here... says ''Arrays bound cannot appear in type specifiers

 Dim ds As New DataSet()
        da.Fill(ds, "products")
        SQLConnection.Close()

        Dim oTable As DataTable
        oTable = ds.Tables("products")

        Dim oFile As DataRow
        For Each oFile In oTable.Rows

            ComboBox2.Items.Add(oFile.Item("products"))

        Next
    End Sub

Recommended Answers

All 7 Replies

well you are using long code. please try this

dim con as new sqlconnection("connection string")
con.open()
dim da as new sqldataadapter("select field1,field2 from table1",con)
dim dt as new datatable
da.fill(dt)
combobox1.datasource = dt
combobox1.displaymember = "field1"
combobox1.valuemember = "field2"
con.close()

this will solve your prob :)

Regards

Try using:

Dim conn As New SqlConnection("connString")
Dim query As String = "SELECT * FROM products"
Dim cmd As New SqlCommand(query, conn)
Dim da As SqlDataAdapter = New SqlDataAdaper(cmd)
Dim table As New DataTable("myTable")
da.Fill(table)
'dispose all IDisposable objects...

Thank you for you reply..

I have already try the below code but isn't works, no error no nothing, just an empty comboBox, I have also try with try catch to see errors but nothing... :/ weird so if you have any other suggestions please let me know.. Another quetion Can I connect the combobox with a data sources?? I mean we can connect combobox directly into a data source for example an .mdf file or .sdf file.. Logically there should be a way to connect the combobox if our database is local as on my situation.. I have searching on google and I didn't found anything.. someone that can knows something I would appreciate if helps...

Thank you in advances!!!
Regards

please use this code , if this time you got any prob then next time i will post the example solution , :P

Sub values()
        Dim con As New SqlConnection("Data Source=waqas\sqlexpress2;Initial Catalog=myDB;Integrated Security=SSPI;")
        con.Open()
        Dim da As New SqlDataAdapter("select name , idno from students", con)
        Dim dt As New DataTable
        da.Fill(dt)
        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "name"
        ComboBox1.ValueMember = "idno"
    End Sub

Well seams to be working now thanks But I must used a button to do that.. Can I load the details from database automatically?? I try to put my code into public form but nothing happents.. Below it's my code::

Dim dt As New DataTable
                Dim DataAdapter2 As New MySqlDataAdapter("SELECT First_Name FROM customers", ServerString)

                DataAdapter2.Fill(dt)
                ComboBox1.DataSource = dt

                ComboBox1.DisplayMember = "First_Name"
                ComboBox1.ValueMember = "id"

what u mean by Automatically ? There is no any user interface? In which event u want this to happen?

i think you want that your combo will show values without pressing a button ? , if yes then just call the code at the load event of your form , or you can also use this code at the combo's got focus event , so when you got focus at combo then it will show your records , if i am wrong then please describe your requirement , and prob .

Regards

commented: Perfect!!!! +1
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.