setting primary key in data table

Hi folks,

I am trying to set a primary key in data table using the following code:

initialData = CType(Session("updateData"), DataSet)
Dim primaryKey(0) As DataColumn
primaryKey(0) = initialData.Tables(0).Columns("ID")
initialData.Tables(0).PrimaryKey = primaryKey

But it keeps giving me error: Object reference not set to an instance of an object.
Would you please kindly advise me what might be wrong with the code?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
put a breakpoint on the first line of the code you've posted. Check to see if InitialData.Tables(0) actually contains a table after you set it to your session variable.

Otherwise, please let us know specifically what line the error is occuring on... or post more of your code so we can see what you're doing.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Are you creating the DataTable on runtime?  Look at the following code from MSDN:

Private Sub SetPrimaryKeys()
   ' Create a new DataTable and set two DataColumn objects as primary keys.
   Dim myTable As DataTable = new DataTable()
   Dim keys(2) As DataColumn
   Dim myColumn  As DataColumn
   ' Create column 1.
   myColumn = New DataColumn()
   myColumn.DataType = System.Type.GetType("System.String")
   myColumn.ColumnName= "FirstName"
   ' Add the column to the DataTable.Columns collection.
   ' Add the column to the array.
   keys(0) = myColumn

   ' Create column 2 and add it to the array.
   myColumn = New DataColumn()
   myColumn.DataType = System.Type.GetType("System.String")
   myColumn.ColumnName = "LastName"
   ' Add the column to the array.
   keys(1) = myColumn
   ' Set the PrimaryKeys property to the array.
   myTable.PrimaryKey = keys
End Sub

HTH, Nauman.
Your primary key assignment looks good. It seems that your first line is not working:

line initialData = CType(Session("updateData"), DataSet)

You might not be getting a DataSet, or you might not have Table(0) created, or it might not have the "ID" column.
IrmawatyAuthor Commented:

Thank you for your assistance. You were right, apparently the table was empty; when I tested it I did not push a button that fills the table with some data.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.