C Sharp ADO.NET - Getting to know Dataset , Datatable objects.-And GridView
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.
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();
}
}
}
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();
dt.TableName = "mytable";
ds.Tables.Add(dt);
string name=dt.TableName;
DataColumn dc = new DataColumn();
DataColumn dc1 = new DataColumn();
dc.ColumnName = "ID";
dc1.ColumnName = "Name";
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
DataRow dr = dt.NewRow();
dr["ID"] = 1;
dr["Name"] = "Karan";
/
dt.Rows.Add(dr);
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();
}
}
}
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>
<!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>
View Code
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);
}
}
}
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()
{
dt.TableName = "mytable";
ds.Tables.Add(dt);
string name = dt.TableName;
DataColumn dc = new DataColumn();
DataColumn dc1 = new DataColumn();
dc.ColumnName = "ID";
dc1.ColumnName = "Name";
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
DataRow dr = dt.NewRow();
dr["ID"] = 1;
dr["Name"] = "Karan";
dt.Rows.Add(dr);
}
}
}
C Sharp ADO.NET - Getting to know Dataset , Datatable objects.-And GridView
Reviewed by LanguageExpert
on
August 25, 2017
Rating:
No comments