user42348
user42348

Reputation: 4319

Error when setting the datasource for a gridview

I have a gridview. Its datasource is set as follows. dtTable is a datable in which the values for the table will be retrieved. But when setting dtTable as datasource I get an error "Object reference not set to an instance of an object". What may be the reason for this?

gridview.Datasource = dtTable
gridview.Databind()

Upvotes: 0

Views: 550

Answers (3)

Phaedrus
Phaedrus

Reputation: 8421

You won't get a null reference exception if the data table is null, you will get one if the gridview is null. That's assuming that one of these two lines are actually generating the exception.

Upvotes: 0

1800 INFORMATION
1800 INFORMATION

Reputation: 135463

Take a look at where the dtTable variable is initialised. Is it coming back as Nothing? Maybe you are getting another error that is being ignored. Probably the dtTable is the cause of the problem.

Upvotes: 1

Rick J
Rick J

Reputation: 2703

Make sure that dtTable has the records you need. This error usually means that dtTable is null.

Upvotes: 1

Related Questions