Monday, 2 December 2013

How create data table in ASP.NET? How to add data to Data table?


In this article I will explain how create datatable in ASP.NET? How to add data to Datatable?

First we need to add below namespace for DataTable.

using System.Data;

After we can declare datatable shown below.

DataTable dtVerifyMobileNo = new DataTable();

Below method is useful for Add columns to the data table.

  void CreateDynamicTableForEmpty()
        {
            dtVerifyMobileNo.Columns.Add("cons_ref");
            dtVerifyMobileNo.Columns.Add("mobile");
            dtVerifyMobileNo.Columns.Add("name");
        }


Below condition is useful for bind data to data table. First we need to declare a Data row for bind data to Datatable.

if (dtXL.Rows[j]["MOBILE"].ToString().Trim().Length == 0)
                                    {
                                        DataRow drMobile = dtVerifyMobileNo.NewRow();
                                        drMobile["cons_ref"] = dtXL.Rows[j]["CONS_REF"].ToString().Trim();
                                        drMobile["mobile"] = dtXL.Rows[j]["MOBILE"].ToString().Trim();
                                        drMobile["name"] = dtXL.Rows[j]["NAME"].ToString().Trim();
                                        dtVerifyMobileNo.Rows.Add(drMobile);
                                    }


No comments:

Post a Comment