Saturday, 28 July 2012

Save Store Files In Sql Database Download From GridView Asp.Net

Upload Save Or Store Files In Ms Sql Server Database And Download In Asp.Net.

In this post i'm explaining how to save pdf,word,excel,jpeg,gif,png files in MS sqlserver database .


Save or Store files in sqlserver database asp.net

I'll also explain how to download files from sql database.

For this example i have created a sample database with table name SaveDoc to save documents in it. table schema is shown below in image.

Read Display Images In GridView From SqlServer DataBase Asp.Net to know how to save and retrieve images in sql database and display them in gridview using handler.




For uploading and saving files to database we need to use Fileupload control, so drag and place one fileupload control and one button on the aspx page in design mode.

Place one label on the page to display success or failure message, and one gridview to display uploaded documents and provide link to download document files.

I have added one button field in gridview to provide download link to file shown in respective row of gridview and this gridview is populated by sqlDataSource.

HTML SOURCE OF THE PAGE
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Button ID="btnUpload" runat="server"  
            onclick="btnUpload_Click" 
            Text="Upload"/>
</div>
<br/>
<asp:Label ID="lblMessage" runat="server">
</asp:Label><br /><br /><br />
      
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataSourceID="SqlDataSource1" 
              onrowcommand="GridView1_RowCommand" 
              DataKeyNames="DocID">
<Columns>
<asp:BoundField DataField="DocID" HeaderText="DocID" 
                InsertVisible="False" 
                ReadOnly="True" 
                SortExpression="DocID" />

<asp:BoundField DataField="DocName" 
                HeaderText="DocName" 
                SortExpression="DocName" />

<asp:BoundField DataField="Type" HeaderText="Type" 
                SortExpression="Type" />
            
<asp:ButtonField ButtonType="Image"  
                ImageUrl="~/download.png" 
                CommandName="Download" 
                HeaderText="Download" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT [DocID], [DocName], [Type] 
               FROM [SaveDoc]">
</asp:SqlDataSource>
</form>

To upload and save files in database write code mentioned below in Click event of upload button we placed on aspx page.

C# CODE
01using System.IO;
02using System.Data;
03using System.Data.SqlClient;
04using System.Configuration;
05 
06protected void btnUpload_Click(object sender, EventArgs e)
07    {
08        //Check whether FileUpload control has file
09        if (FileUpload1.HasFile)
10        {
11            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
12            string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
13            string documentType = string.Empty;
14 
15            //provide document type based on it's extension
16            switch (fileExtension)
17            {
18                case ".pdf":
19                    documentType = "application/pdf";
20                    break;
21                case ".xls":
22                    documentType = "application/vnd.ms-excel";
23                    break;
24                case ".xlsx":
25                    documentType = "application/vnd.ms-excel";
26                    break;
27                case ".doc":
28                    documentType = "application/vnd.ms-word";
29                    break;
30                case ".docx":
31                    documentType = "application/vnd.ms-word";
32                    break;
33                case ".gif":
34                    documentType = "image/gif";
35                    break;
36                case ".png":
37                    documentType = "image/png";
38                    break;
39                case ".jpg":
40                    documentType = "image/jpg";
41                    break;
42            }
43 
44            //Calculate size of file to be uploaded
45            int fileSize = FileUpload1.PostedFile.ContentLength;
46 
47            //Create array and read the file into it
48            byte[] documentBinary = new byte[fileSize];
49            FileUpload1.PostedFile.InputStream.Read(documentBinary, 0, fileSize);
50 
51            // Create SQL Connection
52            SqlConnection con = new SqlConnection();
53            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
54 
55            // Create SQL Command and Sql Parameters
56            SqlCommand cmd = new SqlCommand();
57            cmd.CommandText = "INSERT INTO SaveDoc(DocName,Type,DocData)" +
58                              " VALUES (@DocName,@Type,@DocData)";
59            cmd.CommandType = CommandType.Text;
60            cmd.Connection = con;
61 
62            SqlParameter DocName = new SqlParameter("@DocName", SqlDbType.VarChar, 50);
63            DocName.Value = fileName.ToString();
64            cmd.Parameters.Add(DocName);
65 
66            SqlParameter Type = new SqlParameter("@Type", SqlDbType.VarChar, 50);
67            Type.Value = documentType.ToString();
68            cmd.Parameters.Add(Type);
69 
70            SqlParameter uploadedDocument = new SqlParameter("@DocData", SqlDbType.Binary,fileSize);
71            uploadedDocument.Value = documentBinary;
72            cmd.Parameters.Add(uploadedDocument);
73 
74            con.Open();
75            int result = cmd.ExecuteNonQuery();
76            con.Close();
77            if (result > 0)
78                lblMessage.Text = "File saved to database";
79            GridView1.DataBind();
80        }
81    }
 

To retrieve files from database for download in click of download button we put in gridview, we need to write code mentioned below in RowCommand Event of gridview

c# CODE
01protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
02    {
03        if (e.CommandName == "Download")
04        {
05            string fileName = string.Empty;
06            int index = Convert.ToInt32(e.CommandArgument);
07            GridViewRow row = GridView1.Rows[index];
08            int documentID = Convert.ToInt32(GridView1.DataKeys[index].Value);
09            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
10            SqlCommand cmd = new SqlCommand("SELECT DocName,DocData FROM SaveDoc WHERE DocID = " + documentID, con);
11            con.Open();
12            SqlDataReader dReader = cmd.ExecuteReader();
13            while (dReader.Read())
14            {
15                fileName = dReader["DocName"].ToString();
16                byte[] documentBinary = (byte[])dReader["DocData"];
17                FileStream fStream = new FileStream(Server.MapPath("Docs") + @"\" + fileName, FileMode.Create);
18                fStream.Write(documentBinary, 0, documentBinary.Length);
19                fStream.Close();
20                fStream.Dispose();
21            }
22            con.Close();
23            Response.Redirect(@"Docs\" + fileName);
24        }
25    }

Friday, 27 July 2012

how to download image in asp.net

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


protected void imu_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton imb = (ImageButton)sender;
        string s = Server.MapPath("image//") + imb.CommandArgument.ToString();
        string fName = @s.ToString();
        FileInfo fi = new FileInfo(fName);
        long sz = fi.Length;
        Response.ClearContent();
        Response.ContentType = MimeType(Path.GetExtension(fName));
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(fName)));
        Response.AddHeader("Content-Length", sz.ToString("F0"));
        Response.TransmitFile(fName);
        Response.End();
    }
    public static string MimeType(string Extension)
    {
        string mime = "application/octetstream";
        if (string.IsNullOrEmpty(Extension))
            return mime;
        string ext = Extension.ToLower();
        Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
        if (rk != null && rk.GetValue("Content Type") != null)
            mime = rk.GetValue("Content Type").ToString();
        return mime;
    }

Wednesday, 25 July 2012

gride view sorting,paging,filtering,searching,delete and edit user control

I use two store procedures
this store procedure for fetching data.
ALTER PROCEDURE UCUGrideview
    (
    @pass varchar(max)
    )
AS
    begin
    set nocount on
    exec(@pass)
    set nocount off
    end


this store procedure for delete record

ALTER PROCEDURE UCUGrideviewDelete
    (
    @TABLE varchar(50),
    @COLUMNNAME varchar(50),
    @SCHEMA varchar(50),
    @VALUE varchar(max)
    )
   
AS
 SET NOCOUNT ON
 DECLARE @sql varchar(max)
    begin
    set @sql=('UPDATE  '+@SCHEMA+'.'+@TABLE+' SET Is_Active=0 where '+@COLUMNNAME+' in('+@VALUE+')')
    exec(@sql)
     SET NOCOUNT OFF
    end



DAL code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace .DataAccessLayer
{
    public partial class UERPDataAccess
    {

        DataTable dt = new DataTable();
        public void delete(UCUGrideview obj,string value)
        {
            SqlCommand cm = new SqlCommand("UCUGrideviewDelete");
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TABLE", obj.TABLENAME);
            cm.Parameters.AddWithValue("@COLUMNNAME", obj.COLNAME[0].ToString());
            cm.Parameters.AddWithValue("@SCHEMA", obj.SCHEMA);
            cm.Parameters.AddWithValue("@VALUE", value);
            ExecuteStoredProcedure(cm);
        }
        public DataTable UCUGrideviewdata(UCUGrideview obj)
        {
            int i;
            string s = string.Empty;
         string   ss = string.Empty;
            SqlCommand cm = new SqlCommand("UCUGrideview");
            cm.CommandType = CommandType.StoredProcedure;
            string[] t = obj.COLNAME[0].ToString().Split('=');
            if (t[0].Trim() == "*")
            {
                ss = "select * from " + obj.SCHEMA.ToString() + "." + obj.TABLENAME.ToString() + " where Is_Active=1";
            }
            else
            {
                if (t.GetLength(0) > 1)
                {
                    for (i = 0; i < obj.COLNAME.Count; i++)
                    {
                        string[] tt = obj.COLNAME[i].ToString().Split('=');
                        s = s + tt[0].ToString() + " " + tt[1].ToString() + ",";
                    }

                    ss = "select " + s.Substring(0, s.LastIndexOf(',')) + " from " + obj.SCHEMA.ToString() + "." + obj.TABLENAME.ToString() + " where Is_Active=1";
                }
                else
                {
                    for (i = 0; i < obj.COLNAME.Count; i++)
                        s = s + obj.COLNAME[i].ToString() + ",";
                    ss = "select " + s.Substring(0, s.LastIndexOf(',')) + " from " + obj.SCHEMA.ToString() + "." + obj.TABLENAME.ToString() + " where Is_Active=1";
                }


            }
            cm.Parameters.AddWithValue("@pass", ss);
            dt.Clear();
            dt.Reset();
            dt = ExecuteGetDataTable(cm);
            return dt;
        }

    }
}

user control design code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCUGrideview.ascx.cs" Inherits="Website.App_WebControls.UAMS_UserControls.UCUGrideview" %>


<script language="javascript" type="text/javascript">
    function SelectAllCheckboxes1(chk) {
        var i = 0;
        $('#<%=gv.ClientID%>').find("input:checkbox").each(function () {
            if (this != chk) {
                i = i + 1;
                if (chk.checked)
                    this.checked = true;
                else {
                    this.checked = false;
                    i = 0;
                }
            }
        });
        document.getElementById('<%=sp.ClientID%>').innerHTML = "(" + i + " selected" + ")";
    }
    function SelectAllCheckboxes2()
     {
         $.each($('#<%=gv.ClientID%>').find("tr"), function ()
        {
            var chk = $(this).find("th:first").find(":checkbox");
            chk.attr("checked",false)

        });
        var m = $('#<%=gv.ClientID%>').find("tr").length
        var n = 0;
        $('#<%=gv.ClientID%>').find("input:checkbox").each(function () {
            if (this.checked) {
                n = n + 1;
            }
        });
        document.getElementById('<%=sp.ClientID%>').innerHTML = "(" + n + " selected" + ")";
        if ((parseInt(m) - 1) == parseInt(n))
            $('#<%=gv.ClientID%>').find("th:first").find(":checkbox").attr("checked", true)


    }
    function x() {
        document.getElementById('<%=tbx.ClientID %>').value = "";
    }
    function y() {
       if( document.getElementById('<%=tbx.ClientID %>').value =="")
           document.getElementById('<%=tbx.ClientID %>').value='Search for records'
   }
   function del()
    {
       var i = 0;
       $('#<%=gv.ClientID%>').find("input:checkbox").each(function () {
           if (this.checked)
            {
               i = 1;
           }
       });
       if (i == 0) {
           alert('Please select atleast one item.')
           return false
       }
       else {
           var j = confirm('Do you want to delete it.?');
           if (j == true)
               return true;
           else
               return false;
       }

   }
   function z() {
       if (document.getElementById('<%=tbx.ClientID %>').value == 'Search for records') {
           alert('Please enter a search item.')
           return false;
       }
   }
   function xx() {
       var m = "~`!@#$%^&*()-_+={}[]\|:;\'\"<>,.?/"
       var n = document.getElementById('<%=tbx.ClientID %>')
       for (var i = 0; i < n.value.length; i++)
           if (m.search(n.value.charAt(i)) != -1)
               alert('You can not enter special charecter.');
   }
</script>
<div style="height:365px;width:99.8%;overflow:auto;border:1px solid #C1CDCD">
<table width="100%">
<tr><td colspan="2"><table width="100%"><tr><td align="left" style="width:30%">
    <asp:Label ID="lbheader" runat="server" ForeColor="#666666"></asp:Label></td><td align="center" style="width:40%"><asp:TextBox ID="tbx" Width="200"
        runat="server" ForeColor="#666666" onfocus="x()" onkeyup="xx()" onblur="y()">Search for records</asp:TextBox>&nbsp;<asp:ImageButton
        ID="imbs" runat="server" ImageUrl="~/images/SearchButton.gif"
        onclick="imbs_Click" OnClientClick="return z()" /></td><td align="right" style="width:30%">
        <asp:ImageButton ID="im" onclientclick="return del()"  runat="server"
            ImageUrl="~/Uimages/delete_list.png" onclick="im_Click1" /></td></tr></table></td></tr>

<tr><td colspan="2" align="center"><asp:Label ID="message1" runat="server"
        Visible="False" ForeColor="#666666"></asp:Label>
<asp:GridView ID="gv" runat="server" Width="100%" AllowSorting="True"
        onsorting="gv_Sorting" onrowcreated="gv_RowCreated">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="hcb" onclick="SelectAllCheckboxes1(this)" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cb" onclick="SelectAllCheckboxes2()" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lbtn1" runat="server" onclick="lbtn1_Click" ></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbtn" runat="server" onclick="lbtn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle Font-Underline="true" />
</asp:GridView>
</td></tr>

<tr><td><asp:Label ID="lb2" runat="server" ForeColor="#666666"></asp:Label>&nbsp;<span runat="server"
        id="sp" style="color: #666666">(0 selected)</span></td><td align="right">
    <asp:ImageButton ID="imb" ImageUrl="~/image/PaginFirst.gif" runat="server"
        onclick="imb_Click" ToolTip="First"  />&nbsp;<asp:ImageButton ID="imb2" ImageUrl="~/image/PaginPre.gif" runat="server"
        onclick="imb2_Click" ToolTip="Previous" />
    <asp:Label id="lb" runat="server" ForeColor="#666666"></asp:Label>
    <asp:ImageButton ID="imb1"
        ImageUrl="~/image/PaginNext.gif" ToolTip="Next" runat="server" onclick="imb1_Click" />&nbsp;<asp:ImageButton ID="imb3"
        ImageUrl="~/image/PaginLast.gif" ToolTip="Last" runat="server" onclick="imb3_Click" /></td></tr>
<tr><td colspan="2">
<table width="100%" style=" color:#808080"><tr>
<td><asp:LinkButton ID="lbt" runat="server" Text="All" ToolTip="View all records"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton1" runat="server" Text="#" ToolTip="View records that start with number from 0 to 9"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton2" runat="server" Text="A" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton3" runat="server" Text="B" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton4" runat="server" Text="C" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton5" runat="server" Text="D" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton6" runat="server" Text="E" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton7" runat="server" Text="F" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton8" runat="server" Text="G" ToolTip="View records that start wtih this letter" Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton9" runat="server" Text="H" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton10" runat="server" Text="I" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton11" runat="server" Text="J" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton12" runat="server" Text="K" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton13" runat="server" Text="L" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton14" runat="server" Text="M" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton15" runat="server" Text="N" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton16" runat="server" Text="O" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton17" runat="server" Text="P" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton18" runat="server" Text="Q" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton19" runat="server" Text="R" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton20" runat="server" Text="S" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton21" runat="server" Text="T" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton22" runat="server" Text="U" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton23" runat="server" Text="V" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton24" runat="server" Text="W" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton25" runat="server" Text="X" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton26" runat="server" Text="Y" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton27" runat="server" Text="Z" ToolTip="View records that start wtih this letter"  Font-Underline="false" onclick="lbt_Click"></asp:LinkButton></td>
</tr></table>
</td></tr>
</table>
</div>

user control c# code


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;
using System.Data;



namespace Website.App_WebControls.UAMS_UserControls
{
    public partial class UCUGrideview : System.Web.UI.UserControl
    {
        #region
        int i, j, k, l, m, sk = 0,tk=0,p;
        string s = string.Empty;
        Swash.Objects.UCUGrideview obj1 = new Swash.Objects.UCUGrideview();
        #endregion
        public delegate void open(string s);
        public event open popup;
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
       public void view(Swash.Objects.UCUGrideview obj)
        {
            ViewState["s"] = 0;
            ViewState["ps"] = 10;
            ViewState["m"] = 1;
            ViewState["st"] = 1;
            ViewState["pz"] = 1;
            DataTable ds = new DataTable();
            ds.Clear();
            ds.Reset();
           ds = UERPManagement.GetInstance.UCUGrideviewbusiness(obj);
           ViewState["pk"] = obj.GRIDEHEADER.ToString();
           ViewState["tbn"] = obj.TABLENAME.ToString();
           ViewState["scm"] = obj.SCHEMA.ToString();
           ViewState["clm"] = obj.COLNAME;
           ViewState["ds1"] = ds;
           paging(ds,1);
           lbheader.Text = obj.GRIDEHEADER.ToString() + " : ";
           //gv.DataKeyNames = new string[] { obj.PRIMARYKEYCOLNAME.ToString()};

        }
     
       void paging(DataTable dt, int x)
       {

           if (dt.Rows.Count == 0)
           {
               gv.DataSource = null;
               gv.DataBind();
               message1.Visible = true;
               message1.Text = "No records available that starting with this letter. ";
               lb.Visible = false;
               lb2.Visible = false;
               imb.Visible = false;
               imb1.Visible = false;
               imb2.Visible = false;
               imb3.Visible = false;
               sp.Visible = false;
               im.Visible = false;
               tbx.Text = "Search for records";
               tbx.Visible = false;
               imbs.Visible = false;
               return;
           }
           else
           {
               lb.Visible = true;
               lb2.Visible = true;
               imb.Visible = true;
               imb1.Visible = true;
               imb2.Visible = true;
               imb3.Visible = true;
               sp.Visible = true;
               message1.Visible = false;
               im.Visible = true;
               tbx.Visible = true;
               imbs.Visible = true;
           }
           i = x;
           DataTable dt2 = new DataTable();
           dt2.Clear();
           dt2.Reset();
           ViewState["dt"] = dt;
           tk = Convert.ToInt32(ViewState["ps"]);
           double yy = Math.Ceiling(Convert.ToDouble(dt.Rows.Count) / Convert.ToDouble(ViewState["ps"]));
           if (x == 1)
           {
               sk = 0;
           }
           else
           {
            
          
                   if (x >= Convert.ToInt32(yy))
                   {
                       if (dt.Rows.Count % Convert.ToInt32(ViewState["ps"]) == 0)
                       {
                           x = Convert.ToInt32(yy);
                           sk = ((Convert.ToInt32(ViewState["ps"]) * x) - Convert.ToInt32(ViewState["ps"]));
                       }
                       else
                       {
                           x = Convert.ToInt32(yy);
                           sk = ((Convert.ToInt32(ViewState["ps"]) * x) - Convert.ToInt32(ViewState["ps"]));
                           tk = (dt.Rows.Count) - sk;
                       }
                   }
                   else
                       sk = ((Convert.ToInt32(ViewState["ps"]) * x) - Convert.ToInt32(ViewState["ps"]));
             
             
           }
           dt2 = ((DataTable)ViewState["dt"]).AsEnumerable().Skip(sk).Take(tk).CopyToDataTable();
           if (i > Convert.ToInt32(yy))
           {
               i = Convert.ToInt32(yy);
               ViewState["m"] = i;
           }
           lb.Text = "Page " + i.ToString();
           string st = "";
           if (tk > dt2.Rows.Count)
               st = dt2.Rows.Count.ToString();
           else
               st = tk.ToString();
           if(sk==0)
           lb2.Text = 1 + "-" + st.ToString() + " of " + ((DataTable)ViewState["dt"]).Rows.Count;
           else
               lb2.Text =(1+sk).ToString() + "-" + st.ToString() + " of " + ((DataTable)ViewState["dt"]).Rows.Count;
          gv.DataSource = dt2;
          gv.DataBind();
          for (int m = 0; m < dt2.Rows.Count; m++)
          {
              ((LinkButton)gv.Rows[m].FindControl("lbtn")).CommandArgument = dt2.Rows[m][0].ToString();
              ((LinkButton)gv.Rows[m].FindControl("lbtn")).Text = dt2.Rows[m][1].ToString();

          }
         
         ((LinkButton)gv.HeaderRow.FindControl("lbtn1")).Text = dt.Columns[1].ColumnName.ToString();
             
       }
       protected void lbt_Click(object sender, EventArgs e)
       {
           LinkButton lbtn=(LinkButton)sender;
           DataTable ds = new DataTable();
           ds.Clear();
           ds.Reset();
           ds = ((DataTable)ViewState["ds1"]);
           DataTable dt = new DataTable();
           DataView dv = new DataView();
           dv = ds.DefaultView;
           for (i = 0; i < ds.Columns.Count; i++)
           {
               dt.Columns.Add(ds.Columns[i].ColumnName, typeof(string));
           }
           switch (lbtn.Text)
           {
               case "All":
                   dt = ds;
                   break;
               case "#":
                   for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           for (p = 0; p < 10; p++)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= p.ToString().Length)
                               {
                                   if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == p.ToString().Trim())
                                   {
                                       dv.RowFilter = "Convert(" + ds.Columns[i].ColumnName + ", 'System.String')  like '" + p + "*'";
                                       DataRow dr;
                                       for (k = 0; k < dv.Count; k++)
                                       {
                                           dr = dt.NewRow();
                                           for (l = 0; l < ds.Columns.Count; l++)
                                           {
                                               dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                           }
                                           if (dt.Rows.Count == 0)
                                               dt.Rows.Add(dr);
                                           else
                                           {
                                               int n = 0;
                                               for (m = 0; m < dt.Rows.Count; m++)
                                                   if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                       n = 1;
                                               if (n == 0)
                                                   dt.Rows.Add(dr);
                                           }
                                       }

                                       break;
                                   }
                               }
                             
                           }
                         
                       }

                   }
                   break;
               case "A":
                   for (i = 0; i < ds.Columns.Count; i++)
                   {
                    
                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "A".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "A")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'A*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                  
                   }
                   break;
               case "B":
                   for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "B".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "B")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'B*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "C":
                  for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "C".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "C")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'C*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "D":
                  for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "D".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "D")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'D*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "E":
                   for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "E".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "E")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'E*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "F":
                 for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "F".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "F")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'F*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "G":
                  for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "G".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "G")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'G*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                         
                   }
                   break;
               case "H":
                  for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "H".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "H")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'H*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "I":
            for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "I".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "I")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'I*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                     
                   }
                   break;
               case "J":
                   for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "J".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "J")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'J*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "K":
                  for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "K".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "K")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'K*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "L":
                   for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "L".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "L")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'L*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "M":
                    for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "M".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "M")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'M*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();
                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }
                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "N":
                    for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "N".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "N")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'N*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "O":
                for (i = 0; i < ds.Columns.Count; i++)
                   {
                    
                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                            if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "O".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "O")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'O*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }
                               }
                               break;
                           }
                       }
                     
                   }
                   break;
               case "P":
                   for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "A".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "P")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'P*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "Q":
              for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "Q".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "Q")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'Q*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "R":
                  for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "R".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "R")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'R*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "S":
                  for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "S".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "S")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'S*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "T":
                  for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "T".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "T")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'T*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "U":
                 for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "U".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "U")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'U*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "V":
               for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "V".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "V")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'V*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "W":
                for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "W".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "W")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'W*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "X":
           for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "X".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "X")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'X*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                   }
                   break;
               case "Y":
                   for (i = 0; i < ds.Columns.Count; i++)
                   {

                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "Y".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "Y")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'Y*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }

                       }
                   }
                   break;
               default:
               for (i = 0; i < ds.Columns.Count; i++)
                   {
                     
                       for (j = 0; j < ds.Rows.Count; j++)
                       {
                           if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= "Z".Length)
                           {
                               if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, 1).Trim().ToUpper() == "Z")
                               {
                                   dv.RowFilter = "" + ds.Columns[i].ColumnName + " like 'Z*'";
                                   DataRow dr;
                                   for (k = 0; k < dv.Count; k++)
                                   {
                                       dr = dt.NewRow();
                                       for (l = 0; l < ds.Columns.Count; l++)
                                       {
                                           dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                                       }
                                       if (dt.Rows.Count == 0)
                                           dt.Rows.Add(dr);
                                       else
                                       {
                                           int n = 0;
                                           for (m = 0; m < dt.Rows.Count; m++)
                                               if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                                   n = 1;
                                           if (n == 0)
                                               dt.Rows.Add(dr);
                                       }
                                   }

                                   break;
                               }
                           }
                       }
                     
                   }
                   break;

           }
           ViewState["m"] = 1;
           paging(dt, 1);

       }

       protected void imb_Click(object sender, ImageClickEventArgs e)
       {
           ViewState["m"] = 1;
           paging((DataTable)ViewState["dt"],Convert.ToInt32(ViewState["m"]));
       }

       protected void imb1_Click(object sender, ImageClickEventArgs e)
       {
           ViewState["m"] = Convert.ToInt32(ViewState["m"]) + 1;
           paging((DataTable)ViewState["dt"], Convert.ToInt32(ViewState["m"]));
       }

       protected void imb2_Click(object sender, ImageClickEventArgs e)
       {
           ViewState["m"] = Convert.ToInt32(ViewState["m"]) - 1;
           if (Convert.ToInt32(ViewState["m"]) <= 0)
               ViewState["m"] = 1;
           paging((DataTable)ViewState["dt"], Convert.ToInt32(ViewState["m"]));
       }

       protected void imb3_Click(object sender, ImageClickEventArgs e)
       {
           double yy = Math.Ceiling(Convert.ToDouble(((DataTable)ViewState["dt"]).Rows.Count) / Convert.ToDouble(ViewState["ps"]));
           ViewState["m"] = Convert.ToInt32(yy);
           paging((DataTable)ViewState["dt"], Convert.ToInt32(ViewState["m"]));
       }

       protected void imbs_Click(object sender, ImageClickEventArgs e)
       {
           DataTable ds = new DataTable();
           ds.Clear();
           ds.Reset();
           ds = ((DataTable)ViewState["ds1"]);
           DataTable dt = new DataTable();
           DataView dv = new DataView();
           dv = ds.DefaultView;
           for (i = 0; i < ds.Columns.Count; i++)
           {
               dt.Columns.Add(ds.Columns[i].ColumnName, typeof(string));
           }
           for (i = 0; i < ds.Columns.Count; i++)
           {

               for (j = 0; j < ds.Rows.Count; j++)
               {
                   if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Length >= tbx.Text.Length)
                   {
                       if (ds.Rows[j][ds.Columns[i].ColumnName].ToString().Substring(0, tbx.Text.ToString().Length).Trim().ToUpper() == tbx.Text.ToUpper().Trim())
                       {

                           dv.RowFilter = "Convert(" + ds.Columns[i].ColumnName + ", 'System.String') like '" + tbx.Text + "%'";
                           DataRow dr;
                           for (k = 0; k < dv.Count; k++)
                           {
                               dr = dt.NewRow();
                               for (l = 0; l < ds.Columns.Count; l++)
                               {
                                   dr[ds.Columns[l].ColumnName] = dv[k][l].ToString();

                               }
                               if (dt.Rows.Count == 0)
                                   dt.Rows.Add(dr);
                               else
                               {
                                   int n = 0;
                                   for (m = 0; m < dt.Rows.Count; m++)
                                       if (dt.Rows[m][0].ToString().Trim() == dr[0].ToString().Trim())
                                           n = 1;
                                   if (n == 0)
                                       dt.Rows.Add(dr);
                               }
                           }

                           break;
                       }

                   }

               }

           }
           ViewState["m"] = 1;
           paging(dt,1);
       }
       
       protected void gv_Sorting(object sender, GridViewSortEventArgs e)
       {
          
           DataTable dt = (DataTable)ViewState["dt"];
           DataView dv = new DataView();
           dv = dt.DefaultView;
           DataTable dt3 = new DataTable();
           for (i = 0; i < dt.Columns.Count; i++)
           {
               dt3.Columns.Add(dt.Columns[i].ColumnName, typeof(string));
           }
           if (Convert.ToInt32(ViewState["s"]) == 0)
           {
               dv.Sort = "" + e.SortExpression.ToString() + " ASC";
               ViewState["s"] = 1;
           }
           else
           {
               dv.Sort = "" + e.SortExpression.ToString() + " DESC";
               ViewState["s"] = 0;
           }
           DataRow dr;
           for (i = 0; i < dv.Count; i++)
           {
               dr = dt3.NewRow();
               for (j = 0; j < dt.Columns.Count; j++)
               {
                   dr[dt.Columns[j].ColumnName.ToString()] = dv[i][j].ToString();
               }
               dt3.Rows.Add(dr);
           }
           paging(dt3,Convert.ToInt32( ViewState["m"]));
       }

     

       protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
       {
           e.Row.Cells[2].Visible = false;
           e.Row.Cells[3].Visible = false;
       }

       protected void lbtn_Click(object sender, EventArgs e)
       {
           LinkButton lbt = (LinkButton)sender;
           if (this.popup != null)
           {
               this.popup(lbt.CommandArgument.ToString());
           }
       }

     

       protected void im_Click1(object sender, ImageClickEventArgs e)
       {
           for (i = 0; i < gv.Rows.Count; i++)
           {
               if (((CheckBox)gv.Rows[i].FindControl("cb")).Checked)
               {
                   s = s + ((LinkButton)gv.Rows[i].FindControl("lbtn")).CommandArgument.ToString() + ",";
               }
           }
           s = s.Substring(0, s.LastIndexOf(','));
           obj1.TABLENAME = ViewState["tbn"].ToString();
           obj1.SCHEMA = ViewState["scm"].ToString();
           obj1.GRIDEHEADER = ViewState["pk"].ToString();
           obj1.COLNAME = (ArrayList)ViewState["clm"];
           lbheader.Text = obj1.GRIDEHEADER.ToString() + " : ";
           UERPManagement.GetInstance.delete(obj1, s);
           DataTable ds1 = new DataTable();
           ds1 = UERPManagement.GetInstance.UCUGrideviewbusiness(obj1);
           ViewState["ds1"] = ds1;
           ViewState["dt"] = ds1;
           paging(((DataTable)ViewState["dt"]), Convert.ToInt32(ViewState["m"]));
       }

       protected void lbtn1_Click(object sender, EventArgs e)
       {
           LinkButton lbtt = (LinkButton)sender;
           DataTable dt = (DataTable)ViewState["dt"];
           DataView dv = new DataView();
           dv = dt.DefaultView;
           DataTable dt3 = new DataTable();
           for (i = 0; i < dt.Columns.Count; i++)
           {
               dt3.Columns.Add(dt.Columns[i].ColumnName, typeof(string));
           }
           if (Convert.ToInt32(ViewState["s"]) == 0)
           {
               dv.Sort = "" + lbtt.Text + " ASC";
               ViewState["s"] = 1;
           }
           else
           {
               dv.Sort = "" + lbtt.Text + " DESC";
               ViewState["s"] = 0;
           }
           DataRow dr;
           for (i = 0; i < dv.Count; i++)
           {
               dr = dt3.NewRow();
               for (j = 0; j < dt.Columns.Count; j++)
               {
                   dr[dt.Columns[j].ColumnName.ToString()] = dv[i][j].ToString();
               }
               dt3.Rows.Add(dr);
           }
           paging(dt3, Convert.ToInt32(ViewState["m"]));
       }
    }
}