Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Populating a ComboBox in C#

Reply
Thread Tools

Populating a ComboBox in C#

 
 
Al Wilkerson
Guest
Posts: n/a
 
      11-14-2004
Hey,

I'm trying to populate a combBox in C# with values from an Access DB table.


myDataAdapter.Fill(myDS);

comboBox1.DataSource = myDS; // My dataSet
comboBox1.DataMember = "Compan Name"; // This is the column display
name
comboBox1.ValueMember = "CompanyName"; // This is the actual column name
that holds the values I want

Error message says: "Argument Exception: Could not bind to the new display
member. Parameter name: newDisplayMember"


What am I doing wrong ?

Thanks,

--
Al


 
Reply With Quote
 
 
 
 
CT
Guest
Posts: n/a
 
      11-14-2004
Hi Al,

Inline, please.

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105

"Al Wilkerson" <> wrote in message
news:z1Dld.503566$mD.127480@attbi_s02...
> Hey,
>
> I'm trying to populate a combBox in C# with values from an Access DB
> table.
>
>
> myDataAdapter.Fill(myDS);
>
> comboBox1.DataSource = myDS; // My dataSet
> comboBox1.DataMember = "Compan Name"; // This is the column display
> name
> comboBox1.ValueMember = "CompanyName"; // This is the actual column
> name that holds the values I want
>


Try this:

ComboBox1.DataSource = myDS.Customers; // ...or whatever the table is
called
ComboBox1.DisplayMember = "Compan Name";
ComboBox1.ValueMember = "CompanyName";

> Error message says: "Argument Exception: Could not bind to the new display
> member. Parameter name: newDisplayMember"
>
>
> What am I doing wrong ?
>
> Thanks,
>
> --
> Al
>



 
Reply With Quote
 
 
 
 
Al Wilkerson
Guest
Posts: n/a
 
      11-14-2004
No, that doesn't work because you can't pass in the table like that.

Al

"CT" <> wrote in message
news:%...
> Hi Al,
>
> Inline, please.
>
> --
> Carsten Thomsen
> Enterprise Development with VS .NET, UML, AND MSF
> http://www.apress.com/book/bookDisplay.html?bID=105
>
> "Al Wilkerson" <> wrote in message
> news:z1Dld.503566$mD.127480@attbi_s02...
>> Hey,
>>
>> I'm trying to populate a combBox in C# with values from an Access DB
>> table.
>>
>>
>> myDataAdapter.Fill(myDS);
>>
>> comboBox1.DataSource = myDS; // My dataSet
>> comboBox1.DataMember = "Compan Name"; // This is the column display
>> name
>> comboBox1.ValueMember = "CompanyName"; // This is the actual column
>> name that holds the values I want
>>

>
> Try this:
>
> ComboBox1.DataSource = myDS.Customers; // ...or whatever the table is
> called
> ComboBox1.DisplayMember = "Compan Name";
> ComboBox1.ValueMember = "CompanyName";
>
>> Error message says: "Argument Exception: Could not bind to the new
>> display member. Parameter name: newDisplayMember"
>>
>>
>> What am I doing wrong ?
>>
>> Thanks,
>>
>> --
>> Al
>>

>
>



 
Reply With Quote
 
james
Guest
Posts: n/a
 
      11-14-2004
I am not good at C# (and not much better in VB.NET) but, here is how I do that in VB.NET:

' connects to main client list BondCXR

conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Password="""";User ID=Admin;Data Source=" & mypath() &
"\Data\BondCXR.mdb;Mode=ReadWrite")

conn.Open()

Try

Catch oledbe As OleDbException

MessageBox.Show(oledbe.Message)

Finally

End Try

If conn.State.ToString() = "Open" Then

comm = New OleDbCommand("Select * from Row Order by [LAST]", conn)

da = New OleDbDataAdapter(comm)

ds = New DataSet("Row")

da.Fill(ds, "Row")

DataGrid1.SetDataBinding(ds, "Row")

' bmb is the Binding Manager

bmb = Me.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)

Dim cm As Integer

cm = ds.Tables(0).Rows.Count - 1

Dim rowCount As Integer = cm

'assumes datasource is a datatable...

Dim colCount As Integer

Dim row As Integer

For row = 0 To rowCount - 1

Dim cellValue As Object = Me.DataGrid1(row, 2) + Me.DataGrid1(row, 3)

ComboBox1.Items.Add((cellValue.ToString() + " "))

Next row

End If

conn.Close()

Me.Text = conn.DataSource.ToString

ComboBox1.Visible = True

End Sub

I think you get the idea. You have to know the number of rows in your datasource. In my case I am using the Datagrid as the
source to populate the combobox. And then you have to step thru each row at the column you want and extract the value at the
cell you are interested in and Add that to your combobox.
Hope this gives you the general idea.
(sorry it's not a C# sample)
james







"Al Wilkerson" <> wrote in message news:z1Dld.503566$mD.127480@attbi_s02...
> Hey,
>
> I'm trying to populate a combBox in C# with values from an Access DB table.
>
>
> myDataAdapter.Fill(myDS);
>
> comboBox1.DataSource = myDS; // My dataSet
> comboBox1.DataMember = "Compan Name"; // This is the column display name
> comboBox1.ValueMember = "CompanyName"; // This is the actual column name that holds the values I want
>
> Error message says: "Argument Exception: Could not bind to the new display member. Parameter name: newDisplayMember"
>
>
> What am I doing wrong ?
>
> Thanks,
>
> --
> Al
>



 
Reply With Quote
 
CT
Guest
Posts: n/a
 
      11-14-2004
Al,

You can pass a table like that if you have a strongly typed DataSet.
Otherwise you could use ComboBox1.DataSource = myDS.Tables[0]

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105

"Al Wilkerson" <> wrote in message
news:ZKLld.38133$5K2.10195@attbi_s03...
> No, that doesn't work because you can't pass in the table like that.
>
> Al
>
> "CT" <> wrote in message
> news:%...
>> Hi Al,
>>
>> Inline, please.
>>
>> --
>> Carsten Thomsen
>> Enterprise Development with VS .NET, UML, AND MSF
>> http://www.apress.com/book/bookDisplay.html?bID=105
>>
>> "Al Wilkerson" <> wrote in message
>> news:z1Dld.503566$mD.127480@attbi_s02...
>>> Hey,
>>>
>>> I'm trying to populate a combBox in C# with values from an Access DB
>>> table.
>>>
>>>
>>> myDataAdapter.Fill(myDS);
>>>
>>> comboBox1.DataSource = myDS; // My dataSet
>>> comboBox1.DataMember = "Compan Name"; // This is the column display
>>> name
>>> comboBox1.ValueMember = "CompanyName"; // This is the actual column
>>> name that holds the values I want
>>>

>>
>> Try this:
>>
>> ComboBox1.DataSource = myDS.Customers; // ...or whatever the table
>> is called
>> ComboBox1.DisplayMember = "Compan Name";
>> ComboBox1.ValueMember = "CompanyName";
>>
>>> Error message says: "Argument Exception: Could not bind to the new
>>> display member. Parameter name: newDisplayMember"
>>>
>>>
>>> What am I doing wrong ?
>>>
>>> Thanks,
>>>
>>> --
>>> Al
>>>

>>
>>

>
>



 
Reply With Quote
 
Sylvain Lafontaine
Guest
Posts: n/a
 
      11-14-2004
The DataMember should not be a column but a table (or a view). Here the
piece of code that I'm using for a statement like "Select IdOrganisme, Name
from Organismes" :

DataSet ds = new DataSet();

myAdapter.Fill (ds, "Organismes"); // The name of the table is set
explicitly to « Organismes » here.


ComboIdOrganisme.DataSource = ds;

ComboIdOrganisme.DataMember = "Organismes";

ComboIdOrganisme.DataValueField = "IdOrganisme";

ComboIdOrganisme.DataTextField = "Name";

// ComboIdOrganisme.DataTextFormatString = "{0}"; // In case you want some
formatting for your display.

ComboIdOrganisme.DataBind();

.....

Of course, by using statements like « ComboIdOrganisme.DataSource =
ds.Tables ["Organismes"]; »; some of the above statement are no longer
required. You can also populate your combobox explicitly; for exemple:

while (sqlReader1.Read ()) {

ComboIdOrganisme.Items.Add (new ListItem (sqlReader1
["Name"].ToString(), sqlReader1["IdOrganisme"].ToString()));

}

S. L.


"Al Wilkerson" <> wrote in message
news:z1Dld.503566$mD.127480@attbi_s02...
> Hey,
>
> I'm trying to populate a combBox in C# with values from an Access DB
> table.
>
>
> myDataAdapter.Fill(myDS);
>
> comboBox1.DataSource = myDS; // My dataSet
> comboBox1.DataMember = "Compan Name"; // This is the column display
> name
> comboBox1.ValueMember = "CompanyName"; // This is the actual column
> name that holds the values I want
>
> Error message says: "Argument Exception: Could not bind to the new display
> member. Parameter name: newDisplayMember"
>
>
> What am I doing wrong ?
>
> Thanks,
>
> --
> Al
>



 
Reply With Quote
 
Al Wilkerson
Guest
Posts: n/a
 
      11-16-2004
Thank you all, but I figured it out. This worked for me:

ComboBox1.DataSource = myDS;
ComboBox1.DisplayMember = "Company Name"; // Column Display
Name
ComboBox1.ValueMember = "Suppliers.CompanyName"; // tablename.column name

Al

"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
wrote in message news:%...
> The DataMember should not be a column but a table (or a view). Here the
> piece of code that I'm using for a statement like "Select IdOrganisme,
> Name from Organismes" :
>
> DataSet ds = new DataSet();
>
> myAdapter.Fill (ds, "Organismes"); // The name of the table is set
> explicitly to « Organismes » here.
>
>
> ComboIdOrganisme.DataSource = ds;
>
> ComboIdOrganisme.DataMember = "Organismes";
>
> ComboIdOrganisme.DataValueField = "IdOrganisme";
>
> ComboIdOrganisme.DataTextField = "Name";
>
> // ComboIdOrganisme.DataTextFormatString = "{0}"; // In case you want
> some formatting for your display.
>
> ComboIdOrganisme.DataBind();
>
> ....
>
> Of course, by using statements like « ComboIdOrganisme.DataSource =
> ds.Tables ["Organismes"]; »; some of the above statement are no longer
> required. You can also populate your combobox explicitly; for exemple:
>
> while (sqlReader1.Read ()) {
>
> ComboIdOrganisme.Items.Add (new ListItem (sqlReader1
> ["Name"].ToString(), sqlReader1["IdOrganisme"].ToString()));
>
> }
>
> S. L.
>
>
> "Al Wilkerson" <> wrote in message
> news:z1Dld.503566$mD.127480@attbi_s02...
>> Hey,
>>
>> I'm trying to populate a combBox in C# with values from an Access DB
>> table.
>>
>>
>> myDataAdapter.Fill(myDS);
>>
>> comboBox1.DataSource = myDS; // My dataSet
>> comboBox1.DataMember = "Compan Name"; // This is the column display
>> name
>> comboBox1.ValueMember = "CompanyName"; // This is the actual column
>> name that holds the values I want
>>
>> Error message says: "Argument Exception: Could not bind to the new
>> display member. Parameter name: newDisplayMember"
>>
>>
>> What am I doing wrong ?
>>
>> Thanks,
>>
>> --
>> Al
>>

>
>



 
Reply With Quote
 
Waqas Waqas is offline
Junior Member
Join Date: Aug 2012
Posts: 4
 
      08-22-2012
Hi also try this.code..<a href="http://www.codingresolved.com/discussion/58/how-to-fill-combobox-from-database-in-c-sharpc">http://www.codingresolved.com/discussion/58/how-to-fill-combobox-from-database-in-c-sharpc</a>
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox Project: How to put 4 text items in the combobox christopher.m.lusardi@gmail.com Java 5 10-11-2012 09:29 PM
most efficient way of populating a combobox (in maya) astan.chee Astan Python 1 05-04-2012 03:19 AM
Sync databound combobox to current bound record in form dbuchanan ASP .Net 6 02-02-2006 11:42 PM
Populating a combobox from an XML file Billy Smith Javascript 2 07-18-2005 07:38 PM
MY KINGDOM FOR AN HTML COMBOBOX kpg MCSE 16 01-12-2005 10:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57