We Help You To Renovate Your Blogger Blog With Awesomeness!

Showing posts with label Datatable. Show all posts
Showing posts with label Datatable. Show all posts

Friday, April 15, 2016

  • Conevert Datatable to Object Array C# ASP.Net / ASP MVC

    We need to convert the Datatable dt to Array of objects StudentListResponse



     DataTable dt = new DataTable();
     SqlDataAdapter da = new SqlDataAdapter(command);
     da.Fill(dt);

                var list = dt.AsEnumerable()
    .Skip(1)
    .Select(dr =>
            new StudentListResponse
            {
                id = dr["PKStudentID"].ToString(),
                name = dr["StudentName"].ToString(),
                profile_pic = dr["PhotoURL"].ToString()
            }
            ).ToList();



                StudentListResponse[] students = list.ToArray();

    Its very simple as explained in the previous post to convert Datatable to Object list. Just convert the list to Array as highlighted above.



  • Sunday, October 26, 2014

  • Best way to check if a Data Table has a null (SQL) value in it


    Hostgator Offers


     Check Whole table



    If you want to check if a null value exists in the table you can use this method:
    public static bool HasNull(this DataTable table)
    {
        foreach (DataColumn column in table.Columns)
        {
            if (table.Rows.OfType<DataRow>().Any(r => r.IsNull(column)))
                return true;
        }
    
        return false;
    }
     
     
    and use 
     
    table.HasNull(); 


     where ever you need


     Checking Specific column


    Compare the value of the column to the DBNull.Value value to filter and manage null values in whatever way you see fit.

     
     
     
     
    foreach(DataRow row in table.Rows)
    {
        object value = row["ColumnName"];
        if (value == DBNull.Value)
            // do something
        else
            // do something else
    }
     
     
    Check More About DBNull Class
     
     
     
     
  • Copyright @ 2013 Code Snippets.