Friday, 24 February 2012

Gridview Edit Using Cells And Rows in Asp.Net

create a database name log and run the following script
USE [log]
GO
/****** Object:  Table [dbo].[login]    Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 
 CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED 
(
 [Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridedit.aspx.cs" Inherits="gridedit" %>

<!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 id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Gridview edit in asp.net
    </h2>
    <div style="text-align: center">
        <asp:GridView ID="grdedit" runat="server" AutoGenerateColumns="False" OnRowDataBound="grdedit_RowDataBound"
            OnRowCommand="grdedit_RowCommand" Width="249px">
            <Columns>
                <asp:TemplateField HeaderText="User ID">
                    <ItemTemplate>
                        <asp:Label ID="lbluserid" Text='<%#Eval("id") %>' runat="server"></asp:Label><asp:TextBox
                            ID="txtuserid" Visible="false" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Password">
                    <ItemTemplate>
                        <asp:Label ID="lblpswd" Text='<%#Eval("Password") %>' runat="server"></asp:Label><asp:TextBox
                            ID="txtpswd" Visible="false" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="email">
                    <ItemTemplate>
                        <asp:Label ID="lblcount" Text='<%#Eval("Email") %>' runat="server"></asp:Label><asp:TextBox
                            ID="txtcount" Visible="false" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button Text="EDIT" ID="btnedit" Visible="true" runat="server" CommandName="EditOne" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class gridedit : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }

        all();
    }
    protected void bind()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from login ", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grdedit.DataSource = ds;
        grdedit.DataBind();
    }
    protected void all()
    {
        for (int i = 0; i < grdedit.Rows.Count; i++)
        {
            TextBox t1 = (TextBox)grdedit.Rows[i].Cells[0].FindControl("txtuserid");
            t1.Visible = false;
            TextBox t2 = (TextBox)grdedit.Rows[i].Cells[1].FindControl("txtpswd");
            t2.Visible = false;
            TextBox t3 = (TextBox)grdedit.Rows[i].Cells[2].FindControl("txtcount");
            t3.Visible = false;
            Label l1 = (Label)grdedit.Rows[i].Cells[0].FindControl("lbluserid");
            l1.Visible = true;
            Label l2 = (Label)grdedit.Rows[i].Cells[1].FindControl("lblpswd");
            l2.Visible = true;
            Label l3 = (Label)grdedit.Rows[i].Cells[2].FindControl("lblcount");
            l3.Visible = true;
        }
    }
    int k = 0;
    protected void grdedit_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (grdedit.Rows.Count > 0)
        {
            Label l = (Label)grdedit.Rows[k].Cells[0].FindControl("lbluserid");

            if (l.Text == "santosh")
            {
                Button b = (Button)grdedit.Rows[k].Cells[3].FindControl("btnedit");

                b.Visible = true;
            }

            k++;

        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Button btn = (Button)e.Row.FindControl("btnedit");
            if (btn != null)
            {
                btn.CommandArgument = e.Row.RowIndex.ToString();
            }

        }
    }
    protected void grdedit_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditOne")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            TextBox t1 = (TextBox)grdedit.Rows[rowIndex].Cells[0].FindControl("txtuserid");
            t1.Visible = true;
            TextBox t2 = (TextBox)grdedit.Rows[rowIndex].Cells[1].FindControl("txtpswd");
            t2.Visible = true;
            TextBox t3 = (TextBox)grdedit.Rows[rowIndex].Cells[2].FindControl("txtcount");
            t3.Visible = true;
            Label l1 = (Label)grdedit.Rows[rowIndex].Cells[0].FindControl("lbluserid");
            l1.Visible = false;
            Label l2 = (Label)grdedit.Rows[rowIndex].Cells[1].FindControl("lblpswd");
            l2.Visible = false;
            Label l3 = (Label)grdedit.Rows[rowIndex].Cells[2].FindControl("lblcount");
            l3.Visible = false;
            t1.Text = l1.Text;
            t2.Text = l2.Text;
            t3.Text = l3.Text;

        }

    }
}


How to print the gridview in asp.net

create a database name log and run the following script
USE [log]
GO
/****** Object:  Table [dbo].[Student_S]    Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 
 CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED 
(
 [Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="printgrid.aspx.cs" Inherits="printgrid" %>

<!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 id="Head1" runat="server">
 
<script type="text/javascript" language="javascript">
   function CallPrint( strid )
   {
    var prtContent = document.getElementById( strid );
    var WinPrint = window.open('', '', 'left=0,top=0,width=900,height=600,toolbar=1,scrollbars=1,status=0');
    WinPrint.document.write( prtContent.innerHTML );
    WinPrint.document.close();
    WinPrint.focus();
   WinPrint.print();
  
   //WinPrint.close();
   prtContent.innerHTML=strOldOne;
   }
</script>
   <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Gridview Print in asp.net
    </h2>
    <div id="Print_Grid">
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="Sid" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333"
            GridLines="None">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression="id" />
                <asp:BoundField DataField="Sid" HeaderText="Sid" ReadOnly="True" SortExpression="Sid" />
                <asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="Date_Of_join" HeaderText="Date_Of_join" SortExpression="Date_Of_join" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:logConnectionString %>"
            SelectCommand="SELECT [id], [Sid], [Firstname], [LastName], [Date Of join] AS Date_Of_join, [Phone] FROM [Student_S]">
        </asp:SqlDataSource>
        <!-- <asp:HiddenField ID="hndGridViewPrintContent" runat="server" /> -->
    </div>
    <asp:Button Text="Print" runat="server" ID="btnPrint" OnClientClick="CallPrint('Print_Grid')"
        ForeColor="Blue" />
    </form>
</body>

</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class printgrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

gridview search using Textboxs in asp.net

create a database name log and run the following script
USE [log]
GO
/****** Object:  Table [dbo].[Student_S]    Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 
 CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED 
(
 [Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridsearch.aspx.cs" Inherits="gridsearch" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Gridview Search in asp.net
        <br />
    </h2>
    <div>
        <asp:Label ID="Label1" runat="server" Text="ID"></asp:Label>
        <asp:TextBox ID="txtid" runat="server"></asp:TextBox>
        <asp:Label ID="Label2" runat="server" Text="PSWD"></asp:Label>
        <asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
        <asp:Label ID="Label3" runat="server" Text="Count"></asp:Label>
        <asp:TextBox ID="txtcount" runat="server"></asp:TextBox><br /><br />      
        <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" ForeColor="Blue" />
        <br />
        <asp:Label ID="Label4" runat="server" Visible="false"></asp:Label>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class gridsearch : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ToString());
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {

        Label4.Visible = false;

        SqlCommand cmd = new SqlCommand("select * from login", con);
        SqlDataAdapter sadp = new SqlDataAdapter(cmd);
        sadp.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();




    }
  
protected void btnSearch_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    int flag = 0;
    if (txtid.Text == "" && txtpwd.Text == "" && txtcount.Text == "")
    {
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    else if (txtid.Text != "" && txtpwd.Text == "" && txtcount.Text == "")
    {
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (txtid.Text == ds.Tables[0].Rows[i][0].ToString())
            {
                flag = 1;
                SqlCommand cmd1 = new SqlCommand("select id from login where id='" + txtid.Text + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd1);
                da.Fill(dt);
                break;
            }
        }
        if (flag == 1)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
            flag = 0;
        }
        else
        {
            Label4.Visible = true;
            Label4.Text = "no such user id exists";
        }
    }
    else if (txtid.Text != "" && txtpwd.Text != "" && txtcount.Text == "")
    {
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (txtid.Text == ds.Tables[0].Rows[i][0].ToString() && txtpwd.Text == ds.Tables[0].Rows[i][2].ToString())
            {
                flag = 1;
                SqlCommand cmd1 = new SqlCommand("select id,Password from login where id='" + txtid.Text + "' and Password='" + txtpwd.Text + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd1);
                da.Fill(dt);
                break;
            }
        }
        if (flag == 1)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
            flag = 0;
        }
        else
        {
            Label4.Visible = true;
            Label4.Text = "invalid data";
        }
    }
    else if (txtid.Text != "" && txtpwd.Text != "" && txtcount.Text != "")
    {
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {           
            if (txtid.Text == ds.Tables[0].Rows[i][0].ToString() && txtpwd.Text == ds.Tables[0].Rows[i][2].ToString() && txtcount.Text == ds.Tables[0].Rows[i][4].ToString())
            {
                flag = 1;
                SqlCommand cmd1 = new SqlCommand("select UserName,Password,Email from login where id='" + txtid.Text + "' and Password='" + txtpwd.Text + "' and Roles='" + txtcount.Text + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd1);
                da.Fill(dt);
                break;
            }
        }
        if (flag == 1)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
            flag = 0;
        }
        else
        {
            Label4.Visible = true;
            Label4.Text = "invalid data";
        }
    }
    txtcount.Text = "";
    txtid.Text = "";
    txtpwd.Text = "";
}
}


Gridview scroll in Asp.net

USE [log]
GO
/****** Object:  Table [dbo].[Department]    Script Date: 02/24/2012 11:59:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Department](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [department name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


USE [log]
GO
/****** Object:  Table [dbo].[Student_S]    Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 
 CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED 
(
 [Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridscroll.aspx.cs" Inherits="gridscroll" %>

<!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 id="Head1" runat="server">

    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Gridview scrollbar in asp.net
        <br />
    </h2>
    <div>
        <asp:DropDownList ID="ddlgrdbind" runat="server" OnSelectedIndexChanged="ddlgrdbind_SelectedIndexChanged"
            AutoPostBack="true">
            <asp:ListItem Text="Select One" Value="Select"></asp:ListItem>
            <asp:ListItem Text="X" Value="X"></asp:ListItem>
            <asp:ListItem Text="Y" Value="Y"></asp:ListItem>
        </asp:DropDownList>
        <asp:Panel ID="pnlgrd" runat="server" ScrollBars="Vertical" Height="150px" Visible="false"
            Width="200px">
            <asp:GridView ID="grdscrollbar" runat="server" Visible="false" Width="100px">
            </asp:GridView>
        </asp:Panel>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class gridscroll : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ToString());
    DataSet ds = new DataSet();
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ddlgrdbind_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlgrdbind.SelectedItem.Value == "X")
        {
            pnlgrd.Visible = true;
            grdscrollbar.Visible = true;
            SqlCommand cmd = new SqlCommand("select * from Depatment", con);
            SqlDataAdapter sadp = new SqlDataAdapter(cmd);
            sadp.Fill(ds);
            grdscrollbar.DataSource = ds;
            grdscrollbar.DataBind();

        }
        else if (ddlgrdbind.SelectedItem.Value == "Y")
        {
            pnlgrd.Visible = true;
            grdscrollbar.Visible = true;
            SqlCommand cmd = new SqlCommand("select * from login", con);
            SqlDataAdapter sadp = new SqlDataAdapter(cmd);
            sadp.Fill(ds);
            grdscrollbar.DataSource = ds;
            grdscrollbar.DataBind();

        }
    }
}

Gridview in Gridview or nested Gridview in Asp.Net

Create a Database name with log and then Execute the following scripts in sql server
USE [log]
GO
/****** Object:  Table [dbo].[Department]    Script Date: 02/24/2012 11:59:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Department](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [department name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


USE [log]
GO
/****** Object:  Table [dbo].[Student_S]    Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 
 CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED 
(
 [Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridauto.aspx.cs" Inherits="gridauto" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
     <h2 style=" color:blue; font-style:normal ">    
               Gridview in Gridview in asp.net    
 <br />or Nested Gridview
        </h2>
    <div>
        <asp:GridView ID="GridView1" runat="server" 
            OnRowDataBound="GridView1_RowDataBound"  HeaderStyle-ForeColor="Blue"
            AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="4" 
            ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:TemplateField HeaderText="DEPT">
                    <ItemTemplate>
                        <!-- <asp:BoundField HeaderText="Dept" DataField="deptname" runat="server"/> -->
                        <asp:Label ID="deptname1" runat="server" Text=' <%#Eval("department name")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:GridView ID="GridView2" runat="server"  >
                        <RowStyle BackColor="#EFF3FB" />
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle ForeColor="White" BackColor="#507CD1" Font-Bold="True"></HeaderStyle>
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:logConnectionString %>"
            SelectCommand="SELECT [department name] FROM [Department]"></asp:SqlDataSource>
    </div>
    
   
    </form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class gridauto : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ToString());
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }
    string[] a = new string[10];
    int i = 0;
    int m = 0, n = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (GridView1.Rows.Count >= 0)
        {
            m = n;
            Label l = (Label)e.Row.FindControl("deptname1");
            a[i] = l.Text;
            if (l.Text != " ")
            {
                con.Open();
                GridView gv = (GridView)e.Row.FindControl("GridView2");
                ds.Clear();
                SqlCommand cmd = new SqlCommand("select LastName ,Firstname,Phone from Student_S where Department='" + l.Text + "'", con);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                gv.DataSource = ds;
                gv.DataBind();
                con.Close();
            }
        }
       
    }
}
}

Monday, 20 February 2012

String Split in arathamatic operations in Asp.Net

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

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="text" runat="server"></asp:TextBox>
        <asp:Button ID="Button1"
         runat="server" Text="Button" onclick="Button1_Click" />
     <div>   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;

public partial class stringsplit1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
           string newString = text.Text;
     
    char[] separators = new char[] { ' ', '/', '*', '+', '-' };
     
   
    for (int i = 0; i < separators.Length; i++)
    {
    string str = separators[i].ToString();
    newString = newString.Replace(str, "<" + str + "<");
    }
     
    
     
    string[] substrings = newString.Split(new Char[] { '<' });
    var sSkill = "";
    foreach (String str in substrings)
    {
        if (sSkill.Length == 0)
            sSkill = str;
        else
        sSkill = sSkill + "," + str;
       
      
    }
    Label1.Text = " split string= " + sSkill;
    }
       

    }
           
               
    




calculator in Asp.Net using C#

<%@ 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>Untitled Page</title>
    <style type="text/css">
        .style5
        {
            width: 84px;
        }
        .style19
        {
            width: 84px;
            height: 53px;
        }
        .style23
        {
            width: 84px;
            height: 49px;
        }
        .style27
        {
            height: 41px;
        }
        .style29
        {
            width: 93px;
            height: 49px;
        }
        .style30
        {
            width: 93px;
            height: 53px;
        }
        .style31
        {
            width: 93px;
        }
        .style33
        {
            width: 97px;
            height: 49px;
        }
        .style34
        {
            width: 97px;
            height: 53px;
        }
        .style35
        {
            width: 97px;
        }
        .style36
        {
            width: 84px;
            height: 52px;
        }
        .style38
        {
            width: 93px;
            height: 52px;
        }
        .style39
        {
            width: 97px;
            height: 52px;
        }
        .style40
        {
            width: 85px;
            height: 52px;
        }
        .style41
        {
            width: 85px;
            height: 49px;
        }
        .style42
        {
            width: 85px;
            height: 53px;
        }
        .style43
        {
            width: 85px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table style="height: 233px; width: 473px; margin-right: 0px; margin-bottom: 0px">
    <tr><td colspan="5" class="style27"> 
        <asp:TextBox ID="TextBox1" runat="server" Height="49px" Width="468px"></asp:TextBox>
        </td>
       
        </tr>
    <tr><td class="style36">
        <asp:Button ID="Btn7" runat="server" Height="43px" Text="7" 
            Width="84px" onclick="Btn7_Click" />
        </td><td class="style40">
            <asp:Button ID="Btn8" runat="server" Height="43px" Text="8" 
                Width="95px" onclick="Btn8_Click" />
        </td><td class="style38">
            <asp:Button ID="Btn9" runat="server" Height="43px" Text="9" 
                Width="88px" onclick="Btn9_Click" />
        </td>
        <td class="style39">
            <asp:Button ID="Btnmod" runat="server" Height="43px" Text="%" 
                Width="93px" onclick="Btnmod_Click" />
        </td>
        <td rowspan="4"> 
            <asp:Button ID="Btneql" runat="server" Height="201px" Text="=" 
                Width="79px" onclick="Btneql_Click" />
        </td></tr>
    <tr><td class="style23">
        <asp:Button ID="Btn4" runat="server" Height="43px" Text="4" 
            Width="84px" onclick="Btn4_Click" />
        </td><td class="style41">
            <asp:Button ID="Btn5" runat="server" Height="43px" Text="5" 
                Width="94px" onclick="Btn5_Click" />
        </td><td class="style29">
            <asp:Button ID="Btn6" runat="server" Height="43px" Text="6" 
                Width="90px" onclick="Btn6_Click" />
        </td>
        <td class="style33">
            <asp:Button ID="Btndiv" runat="server" Height="43px" Text="/" 
                Width="93px" onclick="Btndiv_Click" />
        </td></tr>
    <tr><td class="style19">
        <asp:Button ID="Btn1" runat="server" Height="43px" Text="1" 
            Width="84px" onclick="Btn1_Click" />
        </td><td class="style42">
            <asp:Button ID="Btn2" runat="server" Height="43px" Text="2" 
                Width="95px" onclick="Btn2_Click" />
        </td><td class="style30">
            <asp:Button ID="Btn3" runat="server" Height="43px" Text="3" 
                Width="89px" onclick="Btn3_Click" />
        </td>
        <td class="style34">
            <asp:Button ID="Btnminus" runat="server" Height="43px" Text="-" 
                Width="94px" onclick="Btnminus_Click" />
        </td></tr>
    <tr><td class="style5">
        <asp:Button ID="Btn0" runat="server" Height="43px" Text="0" 
            Width="84px" onclick="Btn0_Click" />
        </td><td class="style43">
            <asp:Button ID="Btnprm" runat="server" Height="43px" Text="+/-" 
                Width="95px" onclick="Btnprm_Click" />
        </td><td class="style31">
            <asp:Button ID="Btndot" runat="server" Height="43px" Text="." 
                Width="89px" onclick="Btndot_Click" />
        </td>
        <td class="style35"> 
            <asp:Button ID="Btnplus" runat="server" Height="43px" Text="+" 
                Width="93px" onclick="Btnplus_Click" />
        </td> </tr>
        
    </table>
    </div>
    </form>
</body>
</html>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    public int i;
    public Double val1, val2, res;
    static int c;
    protected void Page_Load(object sender, EventArgs e)
    {
        

    }
    protected void Btn1_Click(object sender, EventArgs e)
    {
        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        }
        TextBox1.Text = TextBox1.Text+"1";
    }
    protected void Btn2_Click(object sender, EventArgs e)
    {
        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        }
        TextBox1.Text = TextBox1.Text + "2";
    }
    protected void Btn3_Click(object sender, EventArgs e)
    {
        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        }
        TextBox1.Text = TextBox1.Text + "3";
    }
    protected void Btn4_Click(object sender, EventArgs e)
    {
        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        }
        TextBox1.Text = TextBox1.Text + "4";
    }
    protected void Btn5_Click(object sender, EventArgs e)
    {

        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        } TextBox1.Text = TextBox1.Text + "5";
    }
    protected void Btn6_Click(object sender, EventArgs e)
    {

        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        } TextBox1.Text = TextBox1.Text + "6";
    }
    protected void Btn7_Click(object sender, EventArgs e)
    {

        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        } TextBox1.Text = TextBox1.Text + "7";
    }
    protected void Btn8_Click(object sender, EventArgs e)
    {
        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        } 
        TextBox1.Text = TextBox1.Text + "8";
    }
    protected void Btn9_Click(object sender, EventArgs e)
    {
        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        } 
        TextBox1.Text = TextBox1.Text + "9";
    }
    protected void Btn0_Click(object sender, EventArgs e)
    {
        if (c == 1)
        {
            TextBox1.Text = "";
            c = 0;
        } 
        TextBox1.Text = TextBox1.Text + "0";
    }
    protected void Btndot_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + ".";
    }
    protected void Btnplus_Click(object sender, EventArgs e)
    {
       ViewState[" value1"] = Convert.ToDouble(TextBox1.Text);
        TextBox1.Text = " ";
        ViewState["j"] = 1;
    }
    protected void Btnminus_Click(object sender, EventArgs e)
    {
        ViewState[" value1"] = Convert.ToDouble(TextBox1.Text);
        TextBox1.Text = " ";
        ViewState["j"] = 2;
    }
    protected void Btndiv_Click(object sender, EventArgs e)
    {
        ViewState[" value1"] = Convert.ToDouble(TextBox1.Text);
        TextBox1.Text = " ";
        ViewState["j"] = 3;
    }
    protected void Btnmod_Click(object sender, EventArgs e)
    {
        ViewState[" value1"] = Convert.ToDouble(TextBox1.Text);
        TextBox1.Text = " ";
        ViewState["j"] = 4;
    }
    protected void Btneql_Click(object sender, EventArgs e)
    {
        ViewState[" value2"] = Convert.ToDouble(TextBox1.Text);
        i = Convert.ToInt32(ViewState["j"].ToString());
        if (i == 1)
            res =Convert.ToDouble( ViewState[" value1"].ToString()) +Convert.ToDouble( ViewState[" value2"].ToString());
        else if (i == 2)
            res = Convert.ToDouble(ViewState[" value1"].ToString()) - Convert.ToDouble(ViewState[" value2"].ToString());
        else if (i == 3)
            res = Convert.ToDouble(ViewState[" value1"].ToString()) / Convert.ToDouble(ViewState[" value2"].ToString());
        else if (i == 4)
            res = Convert.ToDouble(ViewState[" value1"].ToString()) % Convert.ToDouble(ViewState[" value2"].ToString());
        
        TextBox1.Text = Convert.ToString(res);
        c = 1;
    }
    protected void Btnprm_Click(object sender, EventArgs e)
    {
        double t;
        t = Convert.ToDouble(TextBox1.Text);

        t = t * (-1);
        TextBox1.Text = Convert.ToString(t);
    }
}