DataTable Column Type

Progress2007

Active member
Joined
Sep 5, 2008
Messages
25
Programming Experience
1-3
Hi

I have a data set in which there is a datatable. Now i want to find that which column is a identity column....

I hv used to check Autoincrement property to false.....

but m not suceed.....

Please tell me how can i do this.
 
An identity seed doesnt have to be a primary key but if in your case they are the same you can get the primary key column of your datatable by calling colPrimayKey = DataTable.PrimaryKey

Or you can itterate thru each of the columns in your table to see if the AutoIncrement property is set to true or false. Below is a small example include a function that will loop thru the table columns and return the name of the column within a table that is set to auto increment.

VB.NET:
Sub Blah

        Dim strIdentity As String = ""

        strIdentity = FindIdentitySeedColumn(DataSet.Tables("TableName"))

        If strIdentity <> "" Then
            'Found Identity column in table
            MessageBox.Show(String.Format("Identity Column: {0}", strIdentity))
        Else
            MessageBox.Show("Table does not have an identity column")
        End If

End Sub


Public Function FindIdentitySeedColumn(ByVal dtSource As DataTable) As String

        Dim strResult As String = ""

        'Loop thru each column in table
        For Each col As DataColumn In dtSource.Columns
            If col.AutoIncrement = True Then
                'Found column that is set to auto increment
                strResult = col.ColumnName
                Exit For
            End If
        Next col

        Return strResult

End Function
 

Latest posts

Back
Top