MY PRINCIPAL :- "THE GLORY OF LIFE ID TO LOVE NOT TO BELOVED...TO GIVE NOT TO GET..TO SERVE NOT TO BE SERVED."

Saturday, November 20, 2010

Data Navigation through dataset in web application...

In last post we have seen the data navigation through dataset in windows application. Now in this post i am going to show u the same navigation but in web application.

Before that we should know that -In web world , we have to deal with the http protocol and that is stateless protocol so we need to maintain the state in our application. Each time whenever we request something from server it gives us that data but what abt next request..? Server does not recognize the request from where it is coming.I mean the next request is treated as the new request. So it is very important to us that to maintain the state during postbacks.unlike the windows applications are state full.

We have the following are some state management techniques to maintain the state. In this post i m not going to explain the full concepts of state management. u will find that in next post with detail description.

1.ViewState
2.Coockies
3.Session
4.Application

    In this post i am going to use the ViewState to maintain the state in application

Design:

Coding:
 
 1. default.aspx(presentation layer)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 40%;
             background: #3399FF;
             font-family:Calibri;
             font-weight:bold;
             color:White;
        }
        .style2
        {
         font-family:Calibri;
             font-weight:bold;
             color:White;  
             background: #3499FF;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style2" align="center">
            <tr>
                <td colspan="2" align="center">
                    Navigation Demo Using ViewState<hr /></td>
            </tr>
            <tr>
                <td>
                    Id:</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Width="157px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Name:</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Width="157px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Surname:</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Width="157px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    City:</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server" Width="157px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" Text="First" onclick="Button1_Click" />
&nbsp;
                    <asp:Button ID="Button2" runat="server" Text="Prev" onclick="Button2_Click" />
&nbsp;
                    <asp:Button ID="Button3" runat="server" Text="Next" onclick="Button3_Click" />
&nbsp;
                    <asp:Button ID="Button4" runat="server" Text="Last" onclick="Button4_Click" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
 


 2. default.aspx.cs(code behind or business logic layer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    helper help;
    int max;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        help = new helper();
        DataSet ds1;
        ds1 = help.getdata();

        max = ds1.Tables["sample"].Rows.Count;
        if (!IsPostBack)
        {
          
            if (ViewState["inc"] == null)
                ViewState["inc"] = 0;

       DataRow drw= help.showrecord(Convert.ToInt32(ViewState["inc"]));

       showdata(drw);
        }
       

       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        //next button_click
        if (Convert.ToInt32(ViewState["inc"]) < max - 1)
        {
            ViewState["inc"] =Convert.ToInt32( ViewState["inc"]) + 1;
           DataRow drw= help.showrecord(Convert.ToInt32(ViewState["inc"]));
           showdata(drw);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //prev button click
        if (Convert.ToInt32(ViewState["inc"]) > 0)
        {
           
            ViewState["inc"] = Convert.ToInt32(ViewState["inc"]) - 1;
            DataRow drw = help.showrecord(Convert.ToInt32(ViewState["inc"]));
            showdata(drw);
        }

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        //last button click
        if (Convert.ToInt32(ViewState["inc"]) < max - 1)
        {
            ViewState["inc"] = max- 1;
           
            DataRow drw = help.showrecord(Convert.ToInt32(ViewState["inc"]));
            showdata(drw);
           
        }
    }

    private void showdata(DataRow drw)
    {
        TextBox1.Text = drw.ItemArray.GetValue(0).ToString();
        TextBox2.Text = drw.ItemArray.GetValue(1).ToString();
        TextBox3.Text = drw.ItemArray.GetValue(2).ToString();
        TextBox4.Text = drw.ItemArray.GetValue(3).ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //first button click
        if (Convert.ToInt32(ViewState["inc"])!=0)
        {
            ViewState["inc"] = 0;
            DataRow drw = help.showrecord(Convert.ToInt32(ViewState["inc"]));
            showdata(drw);
        }
    }
}


 3. dbhelper.cs(data access layer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for helper
/// </summary>
public class helper
{
    private SqlConnection con;
    private SqlDataAdapter da;
    private DataSet ds;
    int maxrows;
    public helper()
    {
       

    }
    public DataSet getdata()
    {
        string strcon = @"Data Source=.;Initial Catalog=PracticeDB;Persist Security Info=True;User ID=sa;Password=123";
        con = new SqlConnection(strcon);
        da = new SqlDataAdapter("select * from sample",con);
        ds = new DataSet();
        da.Fill(ds, "sample");
        return ds;
    }

    public DataRow showrecord(int inc)
    {
        DataRow drow = ds.Tables["sample"].Rows[inc];

        return drow;
    }

   
}

Happy coding....

No comments:

Post a Comment