Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 6th, 2004, 06:10 PM
Registered User
 
Join Date: Aug 2004
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

 
Old August 7th, 2004, 03:47 AM
Friend of Wrox
 
Join Date: Jul 2004
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.:)
 
Old August 7th, 2004, 11:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo 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.
 
Old August 9th, 2004, 12:07 PM
Registered User
 
Join Date: Aug 2004
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

 
Old March 10th, 2006, 04:01 PM
Authorized User
 
Join Date: Feb 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old March 14th, 2006, 05:51 PM
Authorized User
 
Join Date: Feb 2006
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"
 
Old March 16th, 2006, 11:28 AM
Authorized User
 
Join Date: Mar 2006
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
 
Old March 17th, 2006, 12:57 PM
Authorized User
 
Join Date: Feb 2006
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"
 
Old September 25th, 2006, 09:55 PM
Registered User
 
Join Date: Sep 2006
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
 
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.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.