C Sharp ADO.NET - Getting to know Dataset , Datatable objects.-And GridView


DataTable

DataTable is nothing but a representation of Database Table in Memory. Using the DataTable , we can retrieve the table from the Database into DataTable and can interact or perform operations on it without having to connect to Database every time. DataTable is an exact representation of the Table in Database. Just like Table in SQL DataTable also has Rows and Columns. It is an virtual representation of Table in SQL to make it easy to perform operations.

DataSet

DataSet can be said as collection of the DataTables. DataSet is an Connectionless entity. We can retrieve the Table from the SQL Server into the DataTable and store this DataTable into DataSet.DataSet is also an Virtual Representation of the Collection of the DataTables. In this post we will be studying C Sharp ADO.NET objects i.e Dataset , Datatable and storing value in datatable
In the green colors , the comments are written which describes what the syntax or line for.



using System;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

namespace DatatablePro

{

           class Program

           {

                   static void Main(string[] args)

                  {

                              DataSet ds = new DataSet();

                              DataTable dt = new DataTable();

                            //Giving name to datatable
                              dt.TableName = "mytable";

                            //adding datatable to dataset
                              ds.Tables.Add(dt);

                            //gets name of datatable
                              string name=dt.TableName;

                             //Creating DataColumn Objects
                               DataColumn dc = new DataColumn();

                               DataColumn dc1 = new DataColumn();

                             //Giving name to columns
                               dc.ColumnName = "ID";

                               dc1.ColumnName = "Name";

                             //adding datacolumns to datatable
                               dt.Columns.Add(dc);

                               dt.Columns.Add(dc1);

                             //creating DataRow object
                               DataRow dr = dt.NewRow();

                             //Storing value in columns
                               dr["ID"] = 1;

                               dr["Name"] = "Karan";

                             //adding row to datatable
                               dt.Rows.Add(dr);

                             //displaying values
                               Console.WriteLine("name of datatable is {0} ", name);

                               Console.WriteLine("Value in Datatable \n ID \t Name \n {0} \t {1}",dt.Rows[0]["ID"],dt.Rows[0]["Name"]);

                               Console.ReadKey();

                  }

          }

}


Output


In this post we will be studying how to bind the DataTable to GridView. GridView is an asp.net object which displays the data in Grid Format. For this , We will be creating ASP.NET form and C Sharp code to the background. You will learn from the basic how to Add ASP.NET form and the code in this post.

Follow the below steps .

Open the Visual Studio.


  • Click on the New Project.

  • Select the Visual C #->Web->ASP.NET Empty Web Application as shown in image.

  • This will open Empty ASP.NET web Application Project.

  • Open the Solution Explorer on the right side of the Project.

  • Right click on it. Select Add->Web form as shown in image.


  • Write the below code.

    First in the Web Form.




    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="bindwithgrid.WebForm1" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" Width="200px" Height="100px"></asp:GridView>
        </div>
        </form>
    </body>
    </html>




    Then right click in the page and select View Code

    Write the below code to the page


    using System;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Data;

    namespace bindwithgrid

    {

              public partial class WebForm1 : System.Web.UI.Page

              {

                        DataSet ds = new DataSet();

                        DataTable dt = new DataTable();

                        protected void Page_Load(object sender, EventArgs e)

                       {

                               PopulateDatatable();

                               GridView1.DataSource=ds.Tables[0];

                               GridView1.DataBind();

                       }

                       public void PopulateDatatable()

                      {

                             //Giving name to datatable

                              dt.TableName = "mytable";

                            //adding datatable to dataset

                              ds.Tables.Add(dt);

                           //gets name of datatable

                             string name = dt.TableName;

                           //Creating DataColumn Objects

                             DataColumn dc = new DataColumn();

                             DataColumn dc1 = new DataColumn();

                           //Giving name to columns

                             dc.ColumnName = "ID";

                             dc1.ColumnName = "Name";

                           //adding datacolumns to datatable

                             dt.Columns.Add(dc);

                             dt.Columns.Add(dc1);

                           //creating DataRow object

                             DataRow dr = dt.NewRow();

                           //Storing value in columns

                             dr["ID"] = 1;

                             dr["Name"] = "Karan";

                           //adding row to datatable

                             dt.Rows.Add(dr);

              }

       }

    }





    Go to Debug-> Start Debugging or press F5 on keyboard

    Output
    C Sharp ADO.NET - Getting to know Dataset , Datatable objects.-And GridView C Sharp ADO.NET - Getting to know Dataset , Datatable objects.-And GridView Reviewed by LanguageExpert on August 25, 2017 Rating: 5

    No comments