Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
VB
' Create two instances of the DataTable object to reference the two tables

Dim tblCategories As DataTable = DS.Tables("Categories")
Dim tblProducts As DataTable = DS.Tables("Products")

' Create two DataColumn objects to reference the columns on which the relation is based

' error message is: Object reference not set to an instance of an object
Dim colCatCategoryID As DataColumn = tblCategories.Columns("CategoryID")
Dim colProdCategoryID As DataColumn = tblProducts.Columns("CategoryID")


What I have tried:

At Dim colCatCategoryID As DataColumn = tblCategories.Columns("CategoryID") I get this msg : Object reference not set to an instance of an object.
Posted
Updated 9-Jul-16 12:44pm
v3
Comments
Michael_Davies 9-Jul-16 12:35pm    
Use the debugger to walk through and check that

Dim tblCategories As DataTable = DS.Tables("Categories")

actually returns a table called Categories, if it does not check your SQL to see why not.
Patrice T 9-Jul-16 16:32pm    
What is the question ? What is the problem?
Member 12575668 9-Jul-16 18:26pm    
You can read it at in What I tried: At Dim colCatCategoryID As DataColumn = tblCategories.Columns("CategoryID") I get this msg : Object reference not set to an instance of an object.

Application ""breaks and an error is shown : Object reference not set to an instance of an object.

1 solution

The error message mean that tblCategories.Columns("CategoryID") is not the expected object.

Use the debugger to see what append.
Make sure the column exist.
Quote:
CategoryID column exists in Category Table.
Yes but you open a table named Categories.

[Update]
Use the debugger to make sure the database opens correctly.
Use the debugger to know where is the fail.
 
Share this answer
 
v3
Comments
Member 12575668 9-Jul-16 19:30pm    
CategoryID column exists in Category Table.
Member 12575668 9-Jul-16 20:22pm    
Correction in my comment. Is not Category but Categories : CategoryID column exists in Categories Table.
Michael_Davies 10-Jul-16 3:58am    
It may exist in the table but does the table exist in the dataset?
Member 12575668 10-Jul-16 10:26am    
Using debugger I get : Object reference not set to an instance of an object.
It exits in the Dataset with DS.Tables("Categories") where DS is the Dataset with Dim DS As New DataSet
an0ther1 10-Jul-16 19:41pm    
Use the immediate window & check the table name in the dataset - you will probably find that you Categories table does not exist.
If you use your immediate window you can enter the following;

? DS.Tables.Count

This should return a number (0, 1, 2 etc)
Then get the actual names in the dataset by doing as follows;

? DS.Tables(0).TableName
? DS.Tables(1).TableName
Keep going until you have a name for each of your tables.

Things to note: Tables in datasets do not necessarily have a name unless you set it when you created the dataset.
Both Table and column names are case sensitive.
Object reference not set to an instance of an object means that whatever you are looking for is not there

Kind Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900