locked
validation on datarow RRS feed

  • Question

  • User-123511764 posted

    i would like to validate for either one of the row is null it will display error.

    If i follow the method i use, it need do copy for a lot of time.

    Please advice any dynamic method thx

    row0 row1 row2 row3 row4
    error null error error error
    error error error error null
    null error error error error
    pass pass pass pass pass
     foreach (DataRow row in dt.Rows)
    {
      if(!string.IsNullOrEmpty(row[0].ToString()) || string.IsNullOrEmpty(row[2].ToString()) || string.IsNullOrEmpty(row[3].ToString()) || string.IsNullOrEmpty(row[5].ToString()) ||
    string.IsNullOrEmpty(row[6].ToString()) || string.IsNullOrEmpty(row[7].ToString()) || string.IsNullOrEmpty(row[8].ToString()) || string.IsNullOrEmpty(row[9].ToString()) ||
    string.IsNullOrEmpty(row[10].ToString()) || string.IsNullOrEmpty(row[11].ToString()) || string.IsNullOrEmpty(row[12].ToString()) )
    {display error}
    else if (row[0].ToString() != string.Empty && row[2].ToString() != string.Empty
    && row[3].ToString() != string.Empty && row[5].ToString() != string.Empty
    && row[6].ToString() != string.Empty && row[7].ToString() != string.Empty
    && row[8].ToString() != string.Empty && row[9].ToString() != string.Empty
    && row[10].ToString() != string.Empty && row[11].ToString() != string.Empty
    && row[12].ToString() != string.Empty){ perform my logic}
    } }
    Monday, August 19, 2019 7:22 AM

All replies

  • User753101303 posted

    Hi,

    You are testing only string columns? The first test of the first column seems wrong?

    The basic idea is that you can use DataTable.Columns to loop on each column and decide how to validate this column. A starting point could be something such as (console sample):

    using System;
    using System.Data;
    using Tools;
    // Could be exposed as an extension method to ease reuse
    namespace Tools
    {
        static class DataRowExtensions
        {
            public static bool HasIsNullOrEmpty(this System.Data.DataRow row)
            {
                for (int i = 0; i < row.Table.Columns.Count; i++)
                {
                    var col = row.Table.Columns[i];
                    // Check only string columns ?
                    if (col.DataType == typeof(string))
                    {
                        if (row.IsNull(col)) return true;
                        if (String.IsNullOrEmpty((string)row[i])) return true;
                    }
                }
                return false;
            }
        }
    }
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var dt = new DataTable();
                dt.Columns.Add("A");
                dt.Columns.Add("B");
                dt.Rows.Add(new string[] { "", "z" });
                dt.Rows.Add(new string[] { "a", "z" });
                dt.Rows.Add(new string[] { "z", null });
                // Shows Skipped, Processed, Skipped
                foreach (DataRow row in dt.Rows)
                {
                    if (row.HasIsNullOrEmpty()) // Just call this function
                    {
                        Console.WriteLine("Skipped");
                    }
                    else
                    {
                        Console.WriteLine("Processed");
                    }
                }
            }
        }
    }
    

    Monday, August 19, 2019 9:29 AM