Populating a ComboBox in C#

Discussion in 'ASP .Net' started by Al Wilkerson, Nov 14, 2004.

  1. Al Wilkerson

    Al Wilkerson Guest

    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
    Al Wilkerson, Nov 14, 2004
    #1
    1. Advertising

  2. Al Wilkerson

    CT Guest

    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
    >
    CT, Nov 14, 2004
    #2
    1. Advertising

  3. Al Wilkerson

    Al Wilkerson Guest

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

    >
    >
    Al Wilkerson, Nov 14, 2004
    #3
  4. Al Wilkerson

    james Guest

    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
    >
    james, Nov 14, 2004
    #4
  5. Al Wilkerson

    CT Guest

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

    >>
    >>

    >
    >
    CT, Nov 14, 2004
    #5
  6. 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
    >
    Sylvain Lafontaine, Nov 14, 2004
    #6
  7. Al Wilkerson

    Al Wilkerson Guest

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

    >
    >
    Al Wilkerson, Nov 16, 2004
    #7
  8. Al Wilkerson

    Waqas

    Joined:
    Aug 22, 2012
    Messages:
    4
    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>
    Waqas, Aug 22, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dbuchanan
    Replies:
    6
    Views:
    4,929
    dbuchanan
    Feb 2, 2006
  2. dbuchanan
    Replies:
    3
    Views:
    678
    Cor Ligthert [MVP]
    Feb 1, 2006
  3. Billy Smith

    Populating a combobox from an XML file

    Billy Smith, Jul 18, 2005, in forum: Javascript
    Replies:
    2
    Views:
    168
  4. astan.chee Astan
    Replies:
    1
    Views:
    265
    Steven D'Aprano
    May 4, 2012
  5. Replies:
    5
    Views:
    462
    Roedy Green
    Oct 11, 2012
Loading...

Share This Page