user2430070
user2430070

Reputation: 1

Getting error Object reference not set to an instance of an object

I'm trying to copy the data from sql table "checkin " to auto fill in on a form in a textbox .. on a button click

DataSet ds = null;

private void button8_Click(object sender, EventArgs e)
{
    tblNamesBS.DataSource = ds.Tables[0];
     textBox2.DataBindings.Add(new Binding("Text", tblNamesBS,"GuestName"));
}

Upvotes: 0

Views: 143

Answers (3)

officert
officert

Reputation: 1232

Like the other guys said you need to instantiate ds before you can use it.

DataSet ds = new DataSet();

Upvotes: 0

Gaston Claret
Gaston Claret

Reputation: 998

You cant use ds before setting it to an object....

Therefore, this call is invalid ds.Tables[0]; -> You are trying to acces Tables from ds, when ds is null

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062975

ds starts off as null, and you show no code that would make ds anything other than null. Thus indeed, ds.Tables[0] will explode with a NullReferenceException.

Make ds something non-null.

Upvotes: 3

Related Questions