Solved

Adding a Primary Key column after a datatable has been created

Posted on 2013-05-13
3
463 Views
Last Modified: 2013-05-14
I'm importing values from a database into a local DataTable.

myDataTable = MyBLL.GetCustomComponents()

After the data has been imported into the datatable I want to add a primary key column called "ID" and then just have it auto-increment from 1 to the number of rows of data found in the datatable  

How can I do this?
0
Comment
Question by:cdemott33
3 Comments
 
LVL 1

Expert Comment

by:muh_abbas
ID: 39163810
Hello  cdemott33,

This article will help you.

http://www.java2s.com/Code/SQLServer/Constraints/CreatingaPrimaryKeyonanExistingTable.htm



Regards,
Abbas
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39164241
One option is to add column to the existing table but do not make it auto increment. Loop through the rows and populate manually.

Other option is demonstrated here

http://stackoverflow.com/questions/4310791/best-way-add-a-new-column-with-sequential-numbering-in-an-existing-data-table
0
 

Author Closing Comment

by:cdemott33
ID: 39164515
Thanks for you help.  I created a function that added the column than looped through the existing rows to added the primary key.

    Private Shared Function CreatePrimaryKeyColumn(ByVal dt As DataTable) As DataTable

        Dim dc As New DataColumn("PRIMARY_KEY")
        dc.DataType = GetType(Int32)

        dt.Columns.Add(dc)

        For i As Integer = 1 To dt.Rows.Count - 1
            dt.Rows(i)("PRIMARY_KEY") = i
        Next

        Return dt
    End Function
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now