Home > DeveloperSection > Forums > How to use Dataset in asp.net
Ankit Singh

Total Post:341

Points:2389
Posted on    January-12-2016 9:34 PM

 C# C#  ADO.Net 
Ratings:


 1 Reply(s)
 243  View(s)
Rate this:
I want to use detaset in asp.net.how to use this.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    January-12-2016 9:44 PM

DataSet is a ado.net components.it is a collection of DataTables. you can use the DataSet type to store many DataTables in a single collection.

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

namespace Forumasp

{

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

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

DataSet ds = CreateDataSet();

GridView1.DataSource = ds.Tables["Customer"];

GridView1.DataBind();

}

}

private DataSet CreateDataSet()

{

DataSet dataset = new DataSet();

DataTable Customer = CreateStudentTable();

dataset.Tables.Add(Customer);

return dataset;

}

private DataTable CreateStudentTable()

{

DataTable Customer = new DataTable("Customer");

AddNewColumn(Customer, "System.Int32", "CustomerID");

AddNewColumn(Customer, "System.String", "CustomerName");

AddNewColumn(Customer, "System.String", "CustomerEmailid");

AddNewRow(Customer, 1, "Surendra", "surendra@yahoo.com");

AddNewRow(Customer, 2, "Mahesh Sharma", "mahesh@gmail.com");

AddNewRow(Customer, 3, "Mayank Singh", "mayank@gmail.com");

AddNewRow(Customer, 4, "Sohel", "sohel@gmail.com");

AddNewRow(Customer,5, "Vikram", "vikram@gmail.com");

return Customer;

}

private void AddNewColumn(DataTable table, string columnType, string columnName)

{

DataColumn column = table.Columns.Add(columnName, Type.GetType(columnType));

}

private void AddNewRow(DataTable table, int id, string name, string city)

{

DataRow newrow = table.NewRow();

newrow["CustomerID"] = id;

newrow["CustomerName"] = name;

newrow["CustomerEmailid"] = city;

table.Rows.Add(newrow);

}

}

}

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataSet.aspx.cs" Inherits="Forumasp.DataSetDemo" %>

<!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" AutoGenerateColumns="False" >

<Columns>

<asp:TemplateField HeaderText="CustomerID">

<ItemTemplate ><%#Eval("CustomerID") %></ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="CustomerName">

<ItemTemplate ><%#Eval("CustomerName") %></ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="E-mailaddress">

<ItemTemplate ><%#Eval("CustomerEmailid") %></ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</div>

</form>

</body>

</html>

 


Modified On Jan-25-2016 06:21:57 AM

Don't want to miss updates? Please click the below button!

Follow MindStick