?
Solved

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

Posted on 2003-02-20
7
Medium Priority
?
302 Views
Last Modified: 2008-03-06
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
Comment
Question by:tolstoyleo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:sillycorn
ID: 7988276
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
 
LVL 1

Author Comment

by:tolstoyleo
ID: 7988525
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 7988559
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 1

Expert Comment

by:sillycorn
ID: 7994350
Yes, both your code and mine are work. It's just different method.
0
 
LVL 1

Author Comment

by:tolstoyleo
ID: 7996062
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 7996710
What about this?

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

0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8107055
PAQ'd and points refunded.  tolstoyleo posted answer in second comment.
 
SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question