none
How do I pass a string variable to the dataTable FIND() ? RRS feed

  • Question

  • Hi;

    How do I pass a string variable to the FIND() of a datatable ?

    With this syntax I get an error about the column name, the vars contents is ignored.

    aRow = aDataTable.Rows.Find(aString)

    And of course the string literal works like a champ.

    aRow = aDataTable.Rows.Find("ABCD")

    Thanks for your insights !

     


    GeeMann
    Wednesday, July 6, 2011 7:18 PM

Answers

  • Hi GeeMann;

    The DataTable.Rows.Find method finds the row with the primary key in the table and not any column. In order to set the primary key of a table if not already set is as follows.

    aDataTable.PrimaryKey = new DataColumn[] {aDataTable.Columns["Column Name As String"]};

    Any column can be set as a primary key but if the column is NOT unique you will get an error at run time.

    DataRowCollection.Find Method (Object)

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by GeeMann Wednesday, July 6, 2011 10:18 PM
    Wednesday, July 6, 2011 8:03 PM

All replies

  • Hi GeeMann;

    The DataTable.Rows.Find method finds the row with the primary key in the table and not any column. In order to set the primary key of a table if not already set is as follows.

    aDataTable.PrimaryKey = new DataColumn[] {aDataTable.Columns["Column Name As String"]};

    Any column can be set as a primary key but if the column is NOT unique you will get an error at run time.

    DataRowCollection.Find Method (Object)

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by GeeMann Wednesday, July 6, 2011 10:18 PM
    Wednesday, July 6, 2011 8:03 PM
  • Thanks
    GeeMann
    Wednesday, July 6, 2011 10:19 PM
  • Not a problem, as always glad to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, July 7, 2011 3:18 AM