Saturday, February 25, 2012

Newbie - SqlConnection statement

I'm trying to get a combobox to fill with values from a table. I've
set up an example using the Northwind database with the Categories
table. Dragging in the CategoryID field onto my form as a combobox,
I've set the following properties for the CategoryID combobox:
Value member = "CategoriesBindingSource - CategoryID"
Display Member = "CategoriesBindingSource - CategoryID"
This seems to bring in the correct values in the two non-index fields
(CategoryName and Description). I've then added the following code to
the combobox:
Dim Conn As SqlConnection
Conn = New SqlConnection("Database=Northwnd.mdf")
'Conn.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter("SELECT
CategoryID FROM Categories", Conn)
Dim ds As New DataSet
da.Fill(ds, "Categories")
CategoryIDComboBox.DataSource = ds
'CategoryIDComboBox.ValueMember = "CategoryID"
'CategoryIDComboBox.DisplayMember = "CategoryID"
End Sub
The lines that are commented out are other ideas that I've tried to no
avail. I left them here in case they are relevant.
I suspect that at least part of my problem is in the SqlDataAdapter
statement where I am pointing to the database. I am working on a
standalone pc.
Can anybody see where I am going wrong?
Thanks,
RandyHi Randy
"Randy" wrote:
> I'm trying to get a combobox to fill with values from a table. I've
> set up an example using the Northwind database with the Categories
> table. Dragging in the CategoryID field onto my form as a combobox,
> I've set the following properties for the CategoryID combobox:
> Value member = "CategoriesBindingSource - CategoryID"
> Display Member = "CategoriesBindingSource - CategoryID"
> This seems to bring in the correct values in the two non-index fields
> (CategoryName and Description). I've then added the following code to
> the combobox:
> Dim Conn As SqlConnection
> Conn = New SqlConnection("Database=Northwnd.mdf")
> 'Conn.Open()
> Dim da As SqlDataAdapter = New SqlDataAdapter("SELECT
> CategoryID FROM Categories", Conn)
> Dim ds As New DataSet
> da.Fill(ds, "Categories")
> CategoryIDComboBox.DataSource = ds
> 'CategoryIDComboBox.ValueMember = "CategoryID"
> 'CategoryIDComboBox.DisplayMember = "CategoryID"
> End Sub
> The lines that are commented out are other ideas that I've tried to no
> avail. I left them here in case they are relevant.
> I suspect that at least part of my problem is in the SqlDataAdapter
> statement where I am pointing to the database. I am working on a
> standalone pc.
> Can anybody see where I am going wrong?
> Thanks,
> Randy
>
For connection string information check out
http://www.connectionstrings.com/?carrier=sqlserver2005
You may also want to some of the examples such as
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqlconnectionclasstopic.asp
John

No comments:

Post a Comment