Loading ...

DataTable in ASP.NET | CodeAsp.Net

DataTable in ASP.NET

 /5
0 (0votes)

In this blog I will explain how to create DataTable.

here is my code:-

creating Table first-

private DataTable myTestTable()
    {
        DataTable myTable = new DataTable();
        DataColumn myColumn;

        myColumn = new DataColumn();
        myColumn.DataType = Type.GetType("System.String");
        myColumn.ColumnName = "Name";
        myTable.Columns.Add(myColumn);

        myColumn = new DataColumn();
        myColumn.DataType = Type.GetType("System.String");
        myColumn.ColumnName = "FatherName";
        myTable.Columns.Add(myColumn);

        myColumn = new DataColumn();
        myColumn.DataType = Type.GetType("System.String");
        myColumn.ColumnName = "Comments";
        myTable.Columns.Add(myColumn);
        return myTable;
    }

 

Insert data in table -

private void InsertData(string name, string fatherName, string comments, DataTable dataTable)
    {
        DataRow dataRow;

        dataRow = dataTable.NewRow();

        dataRow["Name"] = name;
        dataRow["FatherName"] = fatherName;
        dataRow["Comments"] = comments;

        dataTable.Rows.Add(dataRow);
    }


page load method

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            DataTable dataTable = new DataTable();
            dataTable = myTestTable();
            InsertData("Mohit", "GopalDahiya", "NA", dataTable);
            Session["tempTable"] = dataTable;

            tstGrdView.DataSource = dataTable;
            tstGrdView.DataBind();
        }
        
    }

 

 

Happy Coding:)

Comments (no comments yet)

Top Posts