locked
validating data in datatable RRS feed

  • Question

  • Hi Guys,

     

    I want some clarity on logic for validating data in datatable. I have a requirement to validate data in datatable and mark that row which has failed in validation.

     

    for example assume there are 3 columns name, age, and phone number. now I have to validate the contents of the phone number column and if it contains string then should add another row say validation and mark it as failed

     

    This will help me keep track of failed rows then I will only have to process the rows which have no errors.

     

    can somebody please let me know how to proceed with this one.
    Sunday, November 20, 2011 3:58 PM

Answers

  •             DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Columns.Add("Age");
                dt.Columns.Add("Phone");
    
                /*
                 * 
                 * Logic to fill the table
                 * 
                 */
                 
                List<DataRow> listOfBadRows = new List<DataRow>();
                foreach (DataRow dr in dt.Rows)
                {
                    if(!int.TryParse(dr["Phone"].ToString()))
                        listOfBadRows.Add(dr);
                }
    
                if (listOfBadRows.Count > 0)
                {
                    /*Few bad rows are there; need to do something*/
                }
                else
                {
                    /*Viola no bad rows...here start processing*/
                }
    


    Thanks,
    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    Dot Net Goodies
    Sunday, November 20, 2011 4:25 PM