Pull multiple values from Column to Combobox

K

Keith

This is 1/2 a SQL Server CE question - 1/2 a
CompactFramework
question.

I'm developing in VB.NET Compact Framework. I want to be
able to pull data out of a SQLCE database on the mobile
device. I've created the .SDF database file - the
application can connect to it. My question comes from a
great deal of inexperience with the SQL query language.

How can I pull a value or number of values out of a
certain column - and populate a combobox with this
values. I'm do understand how to pull the row (or
records) with
a SELECT statement. But once I have those records, how can
I just pull a specific column for all those records and
how can
I take this data and populate a combobox control with it?

thank you.
 
W

William Ryan eMVP

Say that you have a table in CE with the following columns Column1, Column2
and Column3. You want to just populate the combo box with the data of
column2. Say this is your sgl statement. "SELECT Column1, Column2, Column3
from Sometable"

Ok, say that you filled a datatable named 'dt' with the results of that
query.

To bind the combobox, set the datasource...

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Column2";
comboBox1.ValueMember = "Column2";

The display memeber is what will show up in the combobox, the valuemember is
what the selectedvalue will be. they can be the same or they can be
different.

You can also use a datareader and in this instance using the same query:

while rdr.Read(){
comboBox1.Items.Add(rdr.GetString[1]);

}

Either way should work. I used the index 1 b/c that's what corresponds to
Column2.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
K

Keith

William - thank you so much for the reply. I've actually
tried method one - and I'm running into some problems. I
did make a mistake when I said it was a combobox I wanted
to populate - it's actually a list box. In any case - I
tried your suggestion and ran into a few difficulties.

When I execute my code - I get
a "System.Data.DataRowView" - that shows up in the list
box. Shortly afterward - I get an message box
(error) "argumentException" - then the last thing that
happens is the DataGrid is populated with the data.
Here's my code below - do you have any ideas - what I've
done wrong?

Connection = New SqlCeConnection("Data Source=My
Documents\yyg.sdf")
Connection.Open()
Dim SQLCommand As SqlCeCommand =
Connection.CreateCommand
SQLCommand.CommandText = "SELECT first, last
FROM Two"
Dim DataAdapter As New SqlCeDataAdapter
(SQLCommand)
Dim DSet As New DataSet
DataAdapter.Fill(DSet)
DataGrid1.Enabled = False
DataGrid1.DataSource = DSet.Tables(0)
ListBox1.DataSource = DSet.Tables(0)
ListBox1.DisplayMember = "First"
ListBox1.ValueMember = "First"
-----Original Message-----
Say that you have a table in CE with the following columns Column1, Column2
and Column3. You want to just populate the combo box with the data of
column2. Say this is your sgl statement. "SELECT Column1, Column2, Column3
from Sometable"

Ok, say that you filled a datatable named 'dt' with the results of that
query.

To bind the combobox, set the datasource...

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Column2";
comboBox1.ValueMember = "Column2";

The display memeber is what will show up in the combobox, the valuemember is
what the selectedvalue will be. they can be the same or they can be
different.

You can also use a datareader and in this instance using the same query:

while rdr.Read(){
comboBox1.Items.Add(rdr.GetString[1]);

}

Either way should work. I used the index 1 b/c that's what corresponds to
Column2.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
This is 1/2 a SQL Server CE question - 1/2 a
CompactFramework
question.

I'm developing in VB.NET Compact Framework. I want to be
able to pull data out of a SQLCE database on the mobile
device. I've created the .SDF database file - the
application can connect to it. My question comes from a
great deal of inexperience with the SQL query language.

How can I pull a value or number of values out of a
certain column - and populate a combobox with this
values. I'm do understand how to pull the row (or
records) with
a SELECT statement. But once I have those records, how can
I just pull a specific column for all those records and
how can
I take this data and populate a combobox control with it?

thank you.


.
 
W

William Ryan eMVP

Hmmm.. Make sure that the field names match what you use in the Query
exactly. Keep the case consistent and make sure the spelling is correct

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
Keith said:
William - thank you so much for the reply. I've actually
tried method one - and I'm running into some problems. I
did make a mistake when I said it was a combobox I wanted
to populate - it's actually a list box. In any case - I
tried your suggestion and ran into a few difficulties.

When I execute my code - I get
a "System.Data.DataRowView" - that shows up in the list
box. Shortly afterward - I get an message box
(error) "argumentException" - then the last thing that
happens is the DataGrid is populated with the data.
Here's my code below - do you have any ideas - what I've
done wrong?

Connection = New SqlCeConnection("Data Source=My
Documents\yyg.sdf")
Connection.Open()
Dim SQLCommand As SqlCeCommand =
Connection.CreateCommand
SQLCommand.CommandText = "SELECT first, last
FROM Two"
Dim DataAdapter As New SqlCeDataAdapter
(SQLCommand)
Dim DSet As New DataSet
DataAdapter.Fill(DSet)
DataGrid1.Enabled = False
DataGrid1.DataSource = DSet.Tables(0)
ListBox1.DataSource = DSet.Tables(0)
ListBox1.DisplayMember = "First"
ListBox1.ValueMember = "First"
-----Original Message-----
Say that you have a table in CE with the following columns Column1, Column2
and Column3. You want to just populate the combo box with the data of
column2. Say this is your sgl statement. "SELECT Column1, Column2, Column3
from Sometable"

Ok, say that you filled a datatable named 'dt' with the results of that
query.

To bind the combobox, set the datasource...

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Column2";
comboBox1.ValueMember = "Column2";

The display memeber is what will show up in the combobox, the valuemember is
what the selectedvalue will be. they can be the same or they can be
different.

You can also use a datareader and in this instance using the same query:

while rdr.Read(){
comboBox1.Items.Add(rdr.GetString[1]);

}

Either way should work. I used the index 1 b/c that's what corresponds to
Column2.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
This is 1/2 a SQL Server CE question - 1/2 a
CompactFramework
question.

I'm developing in VB.NET Compact Framework. I want to be
able to pull data out of a SQLCE database on the mobile
device. I've created the .SDF database file - the
application can connect to it. My question comes from a
great deal of inexperience with the SQL query language.

How can I pull a value or number of values out of a
certain column - and populate a combobox with this
values. I'm do understand how to pull the row (or
records) with
a SELECT statement. But once I have those records, how can
I just pull a specific column for all those records and
how can
I take this data and populate a combobox control with it?

thank you.


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top