Simple Binding

T

Tony Johansson

Hello!

Here is a simple program where I try to bind myCount which is the number of
rows in table Customers
to a Label's text control named myLabel.
When I run this code I get run time error on the last row saying
"Objektreference has not been set to an instance of the object"

Can somebody tell be what I have missed here ?

public Form()
{
SqlDataAdapter myAdapter;
DataSet myDataSet;
BindingSource myBinding;

string connectionString = @"Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";

myAdapter = new SqlDataAdapter("select count(*) as myCount from
customers", connectionString);
myAdapter.TableMappings.Add("Customers", "Customers");
myDataSet = new DataSet();
myAdapter.Fill(myDataSet, "Customers");
myBinding = new BindingSource(myDataSet, "Customers");
this.myLabel.DataBindings.Add("Text", myBinding, "myCount");
}

//Tony
 
M

Mel Weaver

Hello,
Did you accidentally remove the InitializeComponent() from the
constructor?
 

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