Sunday, August 29, 2010

Access ViewState From ClassFile

teHello Friends

Manytime we need to use ViewState while working 3-tier Architecture but its difficult to use it.
here i have made a sample to use Viewstate from Classfile

Viewstate is an object of Class StateBag

 Here is sample of Viewstate in Class file
in this example we will add names from Textbox to gridview using Viewstate.
in code behind

                Name :
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox><asp:Button ID="btnAdd" runat="server" Text="Add" />
                <asp:GridView ID="grvNames" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <asp:BoundField DataField="ID" HeaderText="No" />
                        <asp:BoundField DataField="SName" HeaderText="Name" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

and in aspx.cs file

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

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        Controller_Default objController = new Controller_Default(this.ViewState);
        btnAdd.Click += new EventHandler(objController.btnAdd_Click);

 Now Create a Controller file named Controller_Default.cs in a Folder named Controller
The Code in the Controller Class will go like this

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

public class Controller_Default
    #region Variables & contructors

    IDictionary _ViewState;
    public Controller_Default() { }

    public Controller_Default(IDictionary objStageBag)
        _ViewState = objStageBag;

    #region Events
    /// Button Event to Add Name 
    ///     ///     public void btnAdd_Click(object sender, EventArgs e)
        Button btn = (Button)sender;
            DataTable dt = new DataTable();
            ViewStateClass objViewState = new ViewStateClass(_ViewState);
            if (objViewState.GetViewStateTable("TblNames") != null)
                dt = objViewState.GetViewStateTable("TblNames");
                dt = CreateNewTable();

            DataRow dr = dt.NewRow();
            dr["ID"] = dt.Rows.Count + 1;
            dr["SName"] = ((TextBox)btn.Parent.FindControl("txtName")).Text.ToString();

            objViewState.SetViewState("TblNames", dt);
            GridView grv = (GridView)btn.Parent.FindControl("grvNames");
            grv.DataSource = dt;

            ((TextBox)btn.Parent.FindControl("txtName")).Text = string.Empty;
        catch (Exception Ex) { }

    #region Methods
    /// Method to Create New Table
    private DataTable CreateNewTable()
        DataTable dt = new DataTable();
        return dt;

in this code we have created and object Named objViewState of class ViewStateClass.

The Syntex objViewState.GetViewStateTable("TblNames") is used to get DataTable from ViewState
Same way  objViewState.SetViewState("TblNames", dt); Saves the DataTable in ViewState.
 Now here is the class ViewStateClass , you can download it from here
 in this class we have many methods to save different types like String,Int, Array and so on ... you can create same whichever you require.
you can download the source from Here

All The Best 

Thursday, August 26, 2010

Get Nth record from Table

Hello Friends Here is a simple query to get nth Record from Table
SELECT @NewPriority=Priority 
                , Priority
                , ROW_NUMBER() OVER (ORDER BY priority) as row 
         FROM Job 
         where IsDeleted=0     
               and Id not in ( select jobId from Job_Cylinder where IsDeleted=0)
       ) a
WHERE row=@cnt-(@NewPriority)+1
There are many ways to get this.. All the Best