HOW TO USE DATASET IN ASP.NET

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  ADO.Net 
C#
 487  View(s)
Ratings:
Rate this:
I want to use detaset in asp.net.how to use this.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to use Dataset in asp.net

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!