Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 6th, 2004, 06:10 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Filling ComboBox from Dataset

Greetings all,

Alas I've been struggling for hours trying to get the results of a query put into a combobox (C# application).

I have been trying to do it like this:

cmbLifeFormID.DataSource = ds.Tables["Lifeform"].Columns["Lifeform"];

Where cmbLifeFormID is my combobox and ds is my dataset containing a table "Lifeform" with only one column "Lifeform". This line is called in the form's constructor.

Any idea of what I am doing wrong, or perhaps a better way to do this?

Regards,
Devon

Reply With Quote
  #2 (permalink)  
Old August 7th, 2004, 03:47 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you cant fill your DataSource with a DataColumn object
use below code instead.
Code:
comboBox1.DataSource=ds.Tables[0];
comboBox1.DisplayMember="Lifeform";
--------------------------------------------
Mehdi.:)
Reply With Quote
  #3 (permalink)  
Old August 7th, 2004, 11:19 PM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik Send a message via Skype™ to melvik
Default

its too easy in design time if u already have DataSet. see the properties of ur combobox & set its DataSet/Display member/ ValueMember.

Always:),
Hovik Melkomian.
Reply With Quote
  #4 (permalink)  
Old August 9th, 2004, 12:07 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you both for your help. I had tried the ds.Tables[0] approach before but I was missing the DisplayMember and ValueMember portion of the puzzle.

Cheers,
Devon

Reply With Quote
  #5 (permalink)  
Old March 10th, 2006, 03:01 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

how to fill combo box using sql dataadapter in c#.net 2005

Reply With Quote
  #6 (permalink)  
Old March 14th, 2006, 04:51 PM
Authorized User
 
Join Date: Feb 2006
Location: Bluefields, , .
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to MAKO
Default

I´m kinda having the same problem. I try to fill a combobox from table in a dataset and receive an error messeage.
I first create and fill the dataset as follows:

string querry = "SQL SELECT statement";
string querry2 = "SQL SELECT statement";
string cn = "Connection string";


DataSet ds = new DataSet();
try
   { using(SqlConnection conn = new SqlConnection(cn);)
       {
        DataAdapter da = new DataAdapter(querry, conn);
        DataAdapter da2 = new DataAdapter(querry2, conn);
        da.Fill(ds, "Customers");
        da2.Fill(ds, "Orders");
       }
    }
catch
    {
    }
finally
    {
   return ds;
    }

then I call filling it with:

combobox1.DataSource = ds.Tables["Customers"];
combobox1.DisplayMember = "CustomerName";
combobox1.ValueMember = "CustomerID";

the error message I get is: "Unable to link to the new Display Member "CustomerID""

MAKO - "El super simio"
Reply With Quote
  #7 (permalink)  
Old March 16th, 2006, 10:28 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

MAKO,
Just a thought as I went through your code. Are you sure that the da.Fill and da2.Fill statements are getting executed. Since they are inside a try-catch block and you are not doing anything within the catch, it might as well be due to system bailing out before it actually does the fill.

Moreover, did u try using typed dataset?

Sreeram
Reply With Quote
  #8 (permalink)  
Old March 17th, 2006, 11:57 AM
Authorized User
 
Join Date: Feb 2006
Location: Bluefields, , .
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to MAKO
Default

Well, actually that is not all of my code. I´ll copy it for you to see and evaluate. I first use functions to create the schema of my dataSet as you can see after I call the DataSet constructor. the rest is the dataset fill and at the end i put the code i use from my windows form to fill the combobox with mydataset.

public DataSet DevuelveBanco()
        {
            DataSet miDataSet = new DataSet();
            CreaTablaBanco(miDataSet);
            CreaTablaPais(miDataSet);
            miDataSet.EnforceConstraints = true;
            string consulta = "SELECT * FROM tbl_Banco";
            string consulta2 = "SELECT * FROM tbl_Pais";
            try
            {
                using(SqlConnection miConexion = new SqlConnection(CadenaConexion))
                {
                    SqlDataAdapter da = new SqlDataAdapter(consulta, miConexion);
                    SqlDataAdapter da2 = new SqlDataAdapter(consulta2, miConexion);
                    da.Fill(miDataSet, "Banco");
                    da2.Fill(miDataSet, "Pais");
                }
                DataColumn parent = miDataSet.Tables["Pais"].Columns["IDPais"];
                DataColumn child = miDataSet.Tables["Banco"].Columns["IDPais"];
                ForeignKeyConstraint fkbanco = new ForeignKeyConstraint("FK_Banco_IDPais", parent, child);
                fkbanco.UpdateRule = Rule.Cascade;
                fkbanco.DeleteRule = Rule.SetNull;
                miDataSet.Tables["Banco"].Constraints.Add(fkbanco);
                miDataSet.Relations.Add("Banco_Pais", miDataSet.Tables["Banco"].Columns["IDPais"], miDataSet.Tables["Pais"].Columns["IDPais"]);
            }
            catch(Exception ex)
            {
                string e = ex.Message;
            }
            return miDataSet;
        }

================================================== ===================

DataSet miDataSet = mante.DevuelveBanco();
txtBanco.DataBindings.Add("Text", miDataSet, "Banco.Nombre");
txtPais.DataBindings.Add("Text", miDataSet, "Pais.Nombre");
cmbPais.DataSource = miDataSet.Tables["Pais"];
cmbPais.DisplayMember = "Nombre";
cmbPais.ValueMember = "IDPais";

MAKO - "El super simio"
Reply With Quote
  #9 (permalink)  
Old September 25th, 2006, 09:55 PM
Registered User
 
Join Date: Sep 2006
Location: lahore, , Pakistan.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SqlConnection objcon = new SqlConnection("Data Source=;Initial Catalog=Project;uid=sa;pwd=;");
        objcon.Open();
        SqlCommand com = new SqlCommand();
        SqlDataAdapter da=new SqlDataAdapter();
        DataSet ds=new DataSet();
        da.TableMappings.Add("Table","category");
        com.Connection=objcon;
        com.CommandType=CommandType.Text;
        com.CommandText="SELECT cat_name FROM category";
        da.SelectCommand=com;
        da.SelectCommand.ExecuteNonQuery();
        da.Fill(ds);
        cmb_cat.DataSource=ds.DefaultViewManager;
        cmb_cat.DisplayMember="category.cat_name";

Aman
Reply With Quote
  #10 (permalink)  
Old June 15th, 2013, 03:31 AM
normantyago
Guest
 
Posts: n/a
Default

sql = "select id,name from authors";

comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";

Filling ComboBox from Dataset ...

norman.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
filling the combobox using sql data adapter kumaran BOOK: Professional .NET 2.0 Generics 4 October 5th, 2015 07:57 AM
Filling a combobox/listbox unmeshraut VB How-To 1 September 21st, 2007 12:08 PM
Filling comboBox from an XML File kuznickic C# 2005 11 August 29th, 2007 01:54 AM
Filling SubForm from ComboBox ayazhoda Access VBA 4 June 5th, 2007 10:56 AM
Filling Combobox with value from other Combobox ayazhoda Access VBA 6 June 5th, 2007 04:58 AM



All times are GMT -4. The time now is 05:34 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.