 |
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
|
|
|

August 6th, 2004, 06:10 PM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

August 7th, 2004, 03:47 AM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
you cant fill your DataSource with a DataColumn object
use below code instead.
Code:
comboBox1.DataSource=ds.Tables[0];
comboBox1.DisplayMember="Lifeform";
--------------------------------------------
Mehdi.:)
|

August 7th, 2004, 11:19 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
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.
|

August 9th, 2004, 12:07 PM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

March 10th, 2006, 04:01 PM
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
how to fill combo box using sql dataadapter in c#.net 2005
|

March 14th, 2006, 05:51 PM
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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"
|

March 16th, 2006, 11:28 AM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

March 17th, 2006, 12:57 PM
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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"
|

September 25th, 2006, 09:55 PM
|
Registered User
|
|
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

June 15th, 2013, 03:31 AM
|
|
sql = "select id,name from authors";
comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
Filling ComboBox from Dataset ...
norman.
|
|
 |