Problem with databinding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Cook

    Problem with databinding

    Hi

    I am new to VS.NET and C#.

    I am trying to do one of the exercises in a book "Teach
    Yourself Visual Studio .NET in 21 Days" and I can't
    figure out what is wrong with the following code:-

    private void Load_Customers( )
    {
    SqlConnection cn = new
    SqlConnection
    (@"Server=
    (local);DataBas e=Northwind;Int egrated Security=SSPI") ;
    SqlDataAdapter da = new
    SqlDataAdapter
    ("SELECT * FROM
    Customers", cn);
    DataSet ds = new DataSet
    ("Customers" );
    da.Fill(ds,"Cus tomers");
    // foreach (DataRow dr in ds.Tables
    ["Customers"].Rows)
    // {
    // MessageBox.Show (dr
    ["CustomerID "].ToString(), "Data",
    //
    MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);

    // } This proved that I could get a
    string of CustomerID's
    comboBox2.DataS ource = ds;
    comboBox2.Value Member
    = "CustomerID "; <<<<<<<< Error here! <<<<
    comboBox2.Displ ayMember
    = "CustomerID ";
    }

    I'm getting an error ...

    "An unhandled exception of
    type 'System.Argumen tException' occurred in
    system.windows. forms.dll
    Additional information: Could not bind to the new display
    member."

    .... on the marked line. The commented out lines were put
    in to prove that I could see the data on the server and
    worked OK.

    Any ideas! It must be simple!

    TIA

    Cheers

    Jeff
  • Jeff Cook

    #2
    Re: Problem with databinding


    "Jeff Cook" <jeffc@aspect.c o.nz> wrote in message
    news:082901c366 ad$69ce29c0$a10 1280a@phx.gbl.. .[color=blue]
    > Hi
    >
    > I am new to VS.NET and C#.
    >
    > I am trying to do one of the exercises in a book "Teach
    > Yourself Visual Studio .NET in 21 Days" and I can't
    > figure out what is wrong with the following code:-
    >[/color]

    My code format got screwed up - here is (hopefully) a more readable
    version - at least in OE

    private void Load_Customers( )
    {
    SqlConnection cn = new SqlConnection
    (@"Server=(loca l);DataBase=Nor thwind;Integrat ed
    Security=SSPI") ;
    SqlDataAdapter da = new SqlDataAdapter
    ("SELECT * FROM Customers", cn);
    DataSet ds = new DataSet("Custom ers");
    da.Fill(ds,"Cus tomers");

    // foreach (DataRow dr in ds.Tables["Customers"].Rows)
    // {
    // MessageBox.Show (dr["CustomerID "].ToString(), "Data",
    // MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);
    // } This proved that I could get a string of CustomerID's

    comboBox2.DataS ource = ds;
    comboBox2.Value Member = "CustomerID "; <<<<<<<< Error here! <<<<
    comboBox2.Displ ayMember = "CustomerID ";
    }

    [color=blue]
    >
    > I'm getting an error ...
    >
    > "An unhandled exception of
    > type 'System.Argumen tException' occurred in
    > system.windows. forms.dll
    > Additional information: Could not bind to the new display
    > member."
    >
    > ... on the marked line. The commented out lines were put
    > in to prove that I could see the data on the server and
    > worked OK.
    >
    > Any ideas! It must be simple!
    >
    > TIA
    >
    > Cheers
    >
    > Jeff[/color]


    Comment

    • Dan Cimpoiesu

      #3
      Re: Problem with databinding

      Try

      comboBox2.DataS ource = ds.Tables["Customers"];
      comboBox2.Value Member = "CustomerID ";
      comboBox2.Displ ayMember = "CustomerID ";

      Regards
      Dan Cimpoiesu

      "Jeff Cook" <jeffcATaspectD OTcoDOTnz> wrote in message
      news:%23WUSq6qZ DHA.1680@tk2msf tngp13.phx.gbl. ..[color=blue]
      >
      > "Jeff Cook" <jeffc@aspect.c o.nz> wrote in message
      > news:082901c366 ad$69ce29c0$a10 1280a@phx.gbl.. .[color=green]
      > > Hi
      > >
      > > I am new to VS.NET and C#.
      > >
      > > I am trying to do one of the exercises in a book "Teach
      > > Yourself Visual Studio .NET in 21 Days" and I can't
      > > figure out what is wrong with the following code:-
      > >[/color]
      >
      > My code format got screwed up - here is (hopefully) a more readable
      > version - at least in OE
      >
      > private void Load_Customers( )
      > {
      > SqlConnection cn = new SqlConnection
      > (@"Server=(loca l);DataBase=Nor thwind;Integrat ed
      > Security=SSPI") ;
      > SqlDataAdapter da = new SqlDataAdapter
      > ("SELECT * FROM Customers", cn);
      > DataSet ds = new DataSet("Custom ers");
      > da.Fill(ds,"Cus tomers");
      >
      > // foreach (DataRow dr in ds.Tables["Customers"].Rows)
      > // {
      > // MessageBox.Show (dr["CustomerID "].ToString(), "Data",
      > // MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);
      > // } This proved that I could get a string of CustomerID's
      >
      > comboBox2.DataS ource = ds;
      > comboBox2.Value Member = "CustomerID "; <<<<<<<< Error here! <<<<
      > comboBox2.Displ ayMember = "CustomerID ";
      > }
      >
      >[color=green]
      > >
      > > I'm getting an error ...
      > >
      > > "An unhandled exception of
      > > type 'System.Argumen tException' occurred in
      > > system.windows. forms.dll
      > > Additional information: Could not bind to the new display
      > > member."
      > >
      > > ... on the marked line. The commented out lines were put
      > > in to prove that I could see the data on the server and
      > > worked OK.
      > >
      > > Any ideas! It must be simple!
      > >
      > > TIA
      > >
      > > Cheers
      > >
      > > Jeff[/color]
      >
      >[/color]


      Comment

      Working...