• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

how to bind combo box to a sql server column in table

ok... this one should be easy, and thank you ahead of time for your help... so far... i have this as my code:

Imports System.Drawing

Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

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

        combo1.DataBindings.Add("text", LoadClients(), "ClientName")

    End Sub

    Private Function LoadClients() As OleDb.OleDbDataReader

        Dim strSQL As String
        strSQL = "select clientname from clients"

        Dim strConn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=SQLOLEDB;Server=IP_ADDR;Database=DB;UID=USERNAME;PWD=PASSWORD")

        Dim myCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSQL, strConn)

        strConn.Open()

        Dim result As OleDb.OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

        Return result

    End Function

End Class



the code is erroring on the OPEN() method.  I think maybe it is because of my connection string?  (i am using the correct IP and UID AND PWD though because I have this connection string in use in a vb6 app... but i'm not quite sure... the error is "unhandled exception" on that line..

Thanks again
0
tolstoyleo
Asked:
tolstoyleo
  • 2
  • 2
  • 2
  • +1
1 Solution
 
sillycornCommented:
You can simple done the binding (not actually binding) to the combobox control:

1. Drop the control to the form (web/win form is just the same) and name it (in my example) cbClient, and dont forget to add Imports System.Data & System.Data.OleDb just below Imports System.Drawing

2. add this below code to the form onload method:
<code>
dim conClient as new OleDbConnection("Provider=SQLOLEDB;Database=Northwind;UID=sa;PWD=asd123")
dim cmdClient as new OleDbCommand("SELECT CategoryName FROM Categories", conClient)
dim DR as OleDbDataReader
       
conClient.Open()
       
DR = cmdClient.ExecuteReader()

while DR.Read()
   dim lst as new ListItem()
   lst.Text = CStr(DR(0))
   cbClient.Items.Add(lst)
end while

conClient.Close()
</code>

hope it works!
0
 
tolstoyleoAuthor Commented:
That is not what i'm trying to do.  Actually I already figured it out.  The correct way to bind is:

Dim dCon As SqlClient.SqlConnection = New SqlClient.SqlConnection("Initial Catalog=sr_interim_camps;Data Source=IP_ADDR;uid=USER_NAME;pwd=PASSWORD")

Dim dAdap As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter("select clientname from clients", dCon)

Dim dt As New DataTable("tbl_clients")

dAdap.Fill(dt)

Dim col As DataColumn

cbo.DataSource = dt

cbo.DisplayMember = "clientname"

cbo.DataBindings.Add("DisplayMember", dt, "clientname")


Thank you anyways though.  Your example probably works fine but I am learning data binding currently.
0
 
iboutchkineCommented:
Create a dataset and fill combo from dataset

Dim cnNorthwind As New SqlConnection("Data Source=localhost;Integrated
Security=SSPI;Initial Catalog=northwind")
Dim cmCustomers As New SqlCommand("uspS_Company", cnNorthwind)
Dim daCustomers As New SqlDataAdapter(cmCustomers)
Dim dsCustomers As New DataSet()
Dim drCustomers As DataRow

'fill DataSet
daCustomers.Fill(dsCustomers, "Customers")

'adding items with DataRow from DataSet
For Each drCustomers In dsCustomers.Tables("Customers").Rows
  cbo.Items.Add(drCustomers("CompanyName"))
Next


If you want to bind to ds then
cbo.DataSource = DataSet1.Customers
cbo.DisplayMember = "CompanyName"

0
[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
sillycornCommented:
Yes, both your code and mine are work. It's just different method.
0
 
tolstoyleoAuthor Commented:
It's NOT A DIFFERENT METHOD!  I AM BINDING DATA! BOTH RESPONSES TO THIS QUESTION ARE JUST LOOPING THROUGH THE RECORD / DATA SET AND ADDING ITEMS ONE BY ONE.  NOTICE THERE IS NO OPEN AND CLOSE IN MINE, AND NO LOOPING!!!!  I AM CLOSING THIS QUESTION, THANK YOU
0
 
iboutchkineCommented:
What about this?

If you want to bind to ds then
cbo.DataSource = DataSet1.Customers
cbo.DisplayMember = "CompanyName"

0
 
SpideyModCommented:
PAQ'd and points refunded.  tolstoyleo posted answer in second comment.
 
SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now