Problem with databinding

J

Jeff Cook

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);DataBase=Northwind;Integrated Security=SSPI");
SqlDataAdapter da = new
SqlDataAdapter
("SELECT * FROM
Customers", cn);
DataSet ds = new DataSet
("Customers");
da.Fill(ds,"Customers");
// foreach (DataRow dr in ds.Tables
["Customers"].Rows)
// {
// MessageBox.Show(dr
["CustomerID"].ToString(), "Data",
//
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

// } This proved that I could get a
string of CustomerID's
comboBox2.DataSource = ds;
comboBox2.ValueMember
= "CustomerID"; <<<<<<<< Error here! <<<<
comboBox2.DisplayMember
= "CustomerID";
}

I'm getting an error ...

"An unhandled exception of
type 'System.ArgumentException' 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
 
J

Jeff Cook

Jeff Cook said:
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:-

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=(local);DataBase=Northwind;Integrated
Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter
("SELECT * FROM Customers", cn);
DataSet ds = new DataSet("Customers");
da.Fill(ds,"Customers");

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

comboBox2.DataSource = ds;
comboBox2.ValueMember = "CustomerID"; <<<<<<<< Error here! <<<<
comboBox2.DisplayMember = "CustomerID";
}
 
D

Dan Cimpoiesu

Try

comboBox2.DataSource = ds.Tables["Customers"];
comboBox2.ValueMember = "CustomerID";
comboBox2.DisplayMember = "CustomerID";

Regards
Dan Cimpoiesu

Jeff Cook said:
Jeff Cook said:
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:-

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=(local);DataBase=Northwind;Integrated
Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter
("SELECT * FROM Customers", cn);
DataSet ds = new DataSet("Customers");
da.Fill(ds,"Customers");

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

comboBox2.DataSource = ds;
comboBox2.ValueMember = "CustomerID"; <<<<<<<< Error here! <<<<
comboBox2.DisplayMember = "CustomerID";
}

I'm getting an error ...

"An unhandled exception of
type 'System.ArgumentException' 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top