473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

populate a comboBox from DataSet

Hi there,

I'm new to C#, and I have a small problem.

I have created a new DataSet using the code below, and I would like to
be able to populat a drop-down list with the results of the "name"
field. Can anyone help me, please?

*************************************************

string m_Connect = "Data Source=(local);integrated
security=SSPI;Initial Catalog=database_name;";
SqlConnection myConn = new SqlConnection(m_Connect);
SqlDataAdapter da = new SqlDataAdapter();
myConn.Open();
DataSet ds1 = new DataSet();
string sQueryString1 = "";
sQueryString1 = "SELECT * FROM sysobjects where type='U'";
da.SelectCommand = new SqlCommand(sQueryString1,myConn);
da.Fill(ds1, "ImportTable");

*************************************************

Thanks,
Marc.
Nov 16 '05 #1
4 14631
comboBox1.DataSource = ds1;
comboBox1.DisplayMember = "Whatever2See";
comboBox1.ValueMember = "Whatever2Have";

Then to bound the combobox to the SelectedValue property using the DataTable
in ds1:

comboBox1.DataBindings.Add("SelectedValue", this.ds1.Tables[0],
"Whatever2Have");

Hope it helps
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Marc Jennings" <marc.nospam..je******@gmail.com> wrote in message
news:48********************************@4ax.com...
Hi there,

I'm new to C#, and I have a small problem.

I have created a new DataSet using the code below, and I would like to
be able to populat a drop-down list with the results of the "name"
field. Can anyone help me, please?

*************************************************

string m_Connect = "Data Source=(local);integrated
security=SSPI;Initial Catalog=database_name;";
SqlConnection myConn = new SqlConnection(m_Connect);
SqlDataAdapter da = new SqlDataAdapter();
myConn.Open();
DataSet ds1 = new DataSet();
string sQueryString1 = "";
sQueryString1 = "SELECT * FROM sysobjects where type='U'";
da.SelectCommand = new SqlCommand(sQueryString1,myConn);
da.Fill(ds1, "ImportTable");

*************************************************

Thanks,
Marc.

Nov 16 '05 #2
Thanks for the quick reply...

If I understood you correctly, if I want to fill the combobox (called
tableslist) with the names of the tables, I should use the following
code....

tableslist.DataSource = ds1;
tableslist.DisplayMember = "name";
tableslist.ValueMember = "name";
tableslist.DataBindings.Add("SelectedValue", ds1.Tables[0],"name");

It is a very simnple app to export the contents of a given table to
XML, so all the data retrieval happens in the same function - hence
the lack of "this."

I have tried the code above and it doesn't populate the list, so I
have to assume I have misunderstood what you meant.

On Thu, 1 Jul 2004 17:17:55 +0300, "Tamir Khason"
<ta**********@tcon-NOSPAM.co.il> wrote:
comboBox1.DataSource = ds1;
comboBox1.DisplayMember = "Whatever2See";
comboBox1.ValueMember = "Whatever2Have";

Then to bound the combobox to the SelectedValue property using the DataTable
in ds1:

comboBox1.DataBindings.Add("SelectedValue", this.ds1.Tables[0],
"Whatever2Have");

Hope it helps


Nov 16 '05 #3
If your select return something like:
name id surname age
john 1 Dow 30
john 2 Brown 34
ann 3 Fox 21

by using this:
tableslist.DataSource = ds1;
tableslist.DisplayMember = "name";
tableslist.ValueMember = "name"; you should recieve combobox of 3 items named john,john and ann with the same
values
while you select one of items and onSelectionChange writes age in other
textbox by using this:tableslist.DataBindings.Add("SelectedValue", ds1.Tables[0],"name"); You will see 21 while select third item

This works fine for me

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Marc Jennings" <marc.nospam..je******@gmail.com> wrote in message
news:e6********************************@4ax.com... Thanks for the quick reply...

If I understood you correctly, if I want to fill the combobox (called
tableslist) with the names of the tables, I should use the following
code....

tableslist.DataSource = ds1;
tableslist.DisplayMember = "name";
tableslist.ValueMember = "name";
tableslist.DataBindings.Add("SelectedValue", ds1.Tables[0],"name");

It is a very simnple app to export the contents of a given table to
XML, so all the data retrieval happens in the same function - hence
the lack of "this."

I have tried the code above and it doesn't populate the list, so I
have to assume I have misunderstood what you meant.

On Thu, 1 Jul 2004 17:17:55 +0300, "Tamir Khason"
<ta**********@tcon-NOSPAM.co.il> wrote:
comboBox1.DataSource = ds1;
comboBox1.DisplayMember = "Whatever2See";
comboBox1.ValueMember = "Whatever2Have";

Then to bound the combobox to the SelectedValue property using the DataTablein ds1:

comboBox1.DataBindings.Add("SelectedValue", this.ds1.Tables[0],
"Whatever2Have");

Hope it helps

Nov 16 '05 #4
Thanks again. I found out where it was all going wrong for me. I had
followed the instruction literally, and not thought to change the
DataSource from ds1 to ds1.Tables[0]

I can't believe I spent so long chasing my tail looking for this
simple answer!!!!

Thanks again.
Marc.

On Thu, 1 Jul 2004 17:43:28 +0300, "Tamir Khason"
<ta**********@tcon-NOSPAM.co.il> wrote:
If your select return something like:
name id surname age
john 1 Dow 30
john 2 Brown 34
ann 3 Fox 21

by using this:
tableslist.DataSource = ds1;
tableslist.DisplayMember = "name";
tableslist.ValueMember = "name";

you should recieve combobox of 3 items named john,john and ann with the same
values
while you select one of items and onSelectionChange writes age in other
textbox by using this:
tableslist.DataBindings.Add("SelectedValue", ds1.Tables[0],"name");

You will see 21 while select third item

This works fine for me


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Goldsworth_Systems | last post by:
I have a string containing valid XML. How do I populate a dataset based on the string, without reference to an XML schema or writing the XML to file and reading it back in again?
1
by: Jassim Rahma | last post by:
Hi, what is the best way to populate comboBox from database.. the reason I am asking is that i have more than 8 comboBoxes in one form and each should retrieve data from a table and I don't want...
7
by: Sharon | last post by:
I have successfully loaded a DataSet object with a XML schema (XSD). Now I wish to populate the tables that was created in the DataSet. I have an XML file/string that contain all the needed data...
3
by: Paul D. Fox | last post by:
I would like to read from Active Directory and populate a dataset to be passed in a Web Service. I can get the data from AD just fine, but can I populate a dataset with these values and do a...
1
by: Sachin | last post by:
Hi, I am working on ASP.Net 2.0 Beta 2 I am having problems creating crystal report using dataset I created a new dataset (DS_ExpCode) and a datatable (DT_ExpCode) under App_Code folder....
2
by: paumierj | last post by:
Hi there Here's my question : I'd like to populate a dataset from an XML string, ie: Dim sXmlString As String sXmlString = "<root type='E-Form'><question label='This is my first...
5
by: John | last post by:
Hi all, I'm sorry I'm reposting this but the original was urgent and I do need closure on this. I'm calling a stored proc which does 4 "selects" and then I populate a dataset looping through...
0
by: vnaz235 | last post by:
Hi, guys, 1. Can anybody help me to populate ComboBox on VB6 form with a) text strings from the range on the already existing Excel sheet (e.g.,...
1
by: freekedoutfish | last post by:
Hi. New member here Im sat at work, pounding my head off the desk because this tiny bit of simple code refuses to work. The sub is intended to pull data from the "companyname" column in the...
1
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
This actually stemmed from anther post I created. But, I have two combo boxes. ComboBox1 is populated by a list of Systems (DataSet from Oracle Query). Now, I want to take Combobox2, and when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.