none
Table doesn't have primary key RRS feed

  • Question

  • I have created a datatable in the following manner. (Note the constraint I've added).

                Dim dt As New DataTable 
     
                dt.Columns.AddRange(New DataColumn() { _ 
                    New DataColumn("SNO"GetType(Integer)), _ 
                    New DataColumn("DESCRIPTION"), _ 
                    New DataColumn("INSERTBY"), _ 
                    New DataColumn("STATUS"), _ 
                    New DataColumn("CAMPAIGN"), _ 
                    New DataColumn("DIALER"), _ 
                    New DataColumn("JOBNAME"), _ 
                    New DataColumn("LIST"), _ 
                    New DataColumn("REMARK")}) 
                dt.Constraints.Add(New UniqueConstraint("PK_SNO", dt.Columns("SNO"))) 
    I fill the table using a dataadapter. The procedure I've used returns the same columns I've defined however there is no primary key for the data that is returned by the procedure. I get an error if I do a:
    dt.Rows.Find(5937)

    PS: 5937 is an example of a key value.

    I am a bundle of mistakes intertwined together with good intentions
    Monday, January 19, 2009 11:34 PM

Answers

  • To create a primary key for a datatable (which will also create a unique constraint)
         dt.PrimaryKey = new DataColumn[] { dt.Columns("SNO") };

    Then you can use dt.Rows.Find(5937)


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    • Marked as answer by deostroll Tuesday, January 20, 2009 3:47 AM
    Tuesday, January 20, 2009 1:00 AM
    Moderator