Monday 22 October 2012

 Download file in asp.net from gridview

 .aspx code
<asp:GridView ID="Grid_Circular_meeting" runat="server" AutoGenerateColumns="False"
                                CellPadding="4" Width="95%" EmptyDataText="No Record Exists!!!" AllowPaging="false"
                                ForeColor="#333333" GridLines="None" DataKeyNames="AutoId" OnRowCommand="Grid_Circular_meeting_RowCommand"
                                OnRowDeleting="Grid_Circular_meeting_RowDeleting" OnSelectedIndexChanged="Grid_Circular_meeting_SelectedIndexChanged">
                                <Columns>
                                    <asp:BoundField HeaderText="Title" DataField="Title" HeaderStyle-HorizontalAlign="Left">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField HeaderText="MeetingDate" DataField="MeetingDate" DataFormatString="{0:dd-MMM-yyyy}"
                                        HeaderStyle-HorizontalAlign="Left">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField HeaderText="Location" DataField="Location" DataFormatString="{0:dd-MMM-yyyy}"
                                        HeaderStyle-HorizontalAlign="Left">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField HeaderText="MeetingTime" DataField="MeetingTime" HeaderStyle-HorizontalAlign="Left">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField HeaderText="File" DataField="FileCName" NullDisplayText="no file" />
                                    <asp:BoundField HeaderText="FilePath" DataField="FilePath" Visible="false" />
                                    <asp:TemplateField HeaderText="Show" HeaderStyle-HorizontalAlign="Left" Visible="true">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="bthshow" runat="server" ImageUrl="~/Images/btnshow.jpg" CommandArgument='<%#Eval("FilePath") %>'
                                                CommandName="download" ToolTip="show" Width="35px" Height="25px" />
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="btndelete" runat="server" ImageUrl="~/Images/close-button.gif"
                                                CommandArgument='<%#Eval("AutoId") %>' CommandName="deleted" ToolTip="show" Width="35px" Height="25px" />
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                </Columns>
                                <AlternatingRowStyle BackColor="White" HorizontalAlign="Left" />
                                <EditRowStyle BackColor="#2461BF" />
                                <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
                                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#EFF3FB" HorizontalAlign="Left" />
                                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                                <SortedDescendingHeaderStyle BackColor="#4870BE" />
                            </asp:GridView>

.CS Code

if (e.CommandName == "download")
            {
                string filename = e.CommandArgument.ToString();
                if (filename != "")
                {
                    string path = MapPath(filename);
                    byte[] bts = System.IO.File.ReadAllBytes(path);
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.AddHeader("Content-Type", "Application/octet-stream");
                    Response.AddHeader("Content-Length", bts.Length.ToString());

                    Response.AddHeader("Content-Disposition", "attachment;   filename=" + filename);

                    Response.BinaryWrite(bts);

                    Response.Flush();

                    Response.End();
                }
            }

Friday 19 October 2012

 Dynamically Add and Delete row in GridView 

.aspx code
-----------
<div>
 <asp:GridView ID="gvBooks" runat="server" ShowFooter="True"
                                AutoGenerateColumns="False" HeaderStyle-CssClass="header-column" onrowcommand="gvBooks_RowCommand"
                                onrowdatabound="gvBooks_RowDataBound" BorderStyle="None" GridLines="None">
                                <Columns>
                                    <asp:TemplateField HeaderText="Book Name">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtBookName" runat="server">
                                            </asp:TextBox>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:LinkButton ID="lbNewBook" runat="server" CausesValidation="false" CommandName="NewBook">New</asp:LinkButton>
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Author">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtAuthor" runat="server" Text='<%#Bind("Author") %>'></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Description">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtDescription" runat="server" Text='<%#Bind("Description") %>'></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Genre">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlGenre" runat="server" AppendDataBoundItems="true">
                                            <asp:ListItem>Choose...</asp:ListItem>
                                            <asp:ListItem>Computer</asp:ListItem>
                                            <asp:ListItem>Fantasy</asp:ListItem>
                                           <asp:ListItem>Horror</asp:ListItem>
                                           <asp:ListItem>Comedy</asp:ListItem>
                                            </asp:DropDownList>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Price">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtPercentage" runat="server" Width="100px" Text='<%#Bind("Price") %>'></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Publish Date">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtPublishDate" runat="server" Width="100px" Text='<%#Bind("PublishDate") %>'></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:ImageButton ID="imgbtnDeleteBook" runat="server" CausesValidation="false" ImageUrl="~/App_Themes/Red/Images/cross.png" CommandName="DeleteBook" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                        </div>
.cs code

public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            AddBookDefault();
        }
    }

    private void AddBookDefault()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("BookName", typeof(string)));
        dt.Columns.Add(new DataColumn("Author", typeof(string)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Genre", typeof(string)));
        dt.Columns.Add(new DataColumn("Price", typeof(string)));
        dt.Columns.Add(new DataColumn("PublishDate", typeof(string)));

        dr = dt.NewRow();

        dr["BookName"] = string.Empty;
        dr["Author"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Genre"] = string.Empty;
        dr["Price"] = string.Empty;
        dr["PublishDate"] = string.Empty;

        dt.Rows.Add(dr);
        //dr = dt.NewRow();
        //Store the DataTable in ViewState

        ViewState["CurrentTable"] = dt;

        gvBooks.DataSource = dt;
        gvBooks.DataBind();
    }
    protected void gvBooks_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int rowIndex = 0;
        DataTable dt = (DataTable)ViewState["CurrentTable"];

        if (e.CommandName == "NewBook")
        {
            DataRow rw = dt.NewRow();
            dt.Rows.Add(rw);

            //Set Previous Data
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count - 1; i++)
                {
                    TextBox txtBookName = (TextBox)gvBooks.Rows[rowIndex].Cells[0].FindControl("txtBookName");
                    TextBox txtAuthor = (TextBox)gvBooks.Rows[rowIndex].Cells[1].FindControl("txtAuthor");
                    TextBox txtDescription = (TextBox)gvBooks.Rows[rowIndex].Cells[2].FindControl("txtDescription");
                    DropDownList ddlGenre = (DropDownList)gvBooks.Rows[rowIndex].Cells[3].FindControl("ddlGenre");
                    TextBox txtPrice = (TextBox)gvBooks.Rows[rowIndex].Cells[4].FindControl("txtPercentage");
                    TextBox txtPublishDate = (TextBox)gvBooks.Rows[rowIndex].Cells[5].FindControl("txtPublishDate");

                    dt.Rows[i]["BookName"] = txtBookName.Text;
                    dt.Rows[i]["Author"] = txtAuthor.Text;
                    dt.Rows[i]["Description"] = txtDescription.Text;
                    dt.Rows[i]["Genre"] = ddlGenre.SelectedItem.Text;
                    dt.Rows[i]["Price"] = txtPrice.Text;
                    dt.Rows[i]["PublishDate"] = txtPublishDate.Text;

                    rowIndex++;
                }
            }
        }
        else if (e.CommandName == "DeleteBook")
        {
            GridViewRow gvr = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
            dt.Rows.RemoveAt(gvr.RowIndex);
        }

        gvBooks.DataSource = dt;
        gvBooks.DataBind();
    }

    protected void gvBooks_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlGenre = (DropDownList)e.Row.Cells[3].FindControl("ddlGenre");

            ddlGenre.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Genre").ToString();
        }
    }

}

Wednesday 17 October 2012


Procedure for find Birthday of employee before one week


1)

select * from TableName where DATEPART(DAYOFYEAR,TableName.DOB) between
DATEPART(DAYOFYEAR,GETDATE()) and DATEPART(DAYOFYEAR,dateadd(day,7,getdate()))

2)


 SELECT * from table name where DATEPART( Week, DATEADD( Year, DATEPART( Year, GETDATE()) -DATEPART( Year, DOB), DOB))= DATEPART( Week, GETDATE());

Saturday 13 October 2012

 Auto Tab Transfer from one textbox to another textbox using Javascript


<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8">
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=TextBox1.ClientID %>").keyup(
            function changefocus() {
                if ($("#<%=TextBox1.ClientID %>").val().length >= $("#<%=TextBox1.ClientID %>").attr('maxlength'))
                    $("#<%=TextBox2.ClientID %>").focus();
            });
            $("#<%=TextBox2.ClientID %>").keyup(
            function changefocus() {
                if ($("#<%=TextBox2.ClientID %>").val().length >= $("#<%=TextBox2.ClientID %>").attr('maxlength'))
                    $("#<%=TextBox3.ClientID %>").focus();
            });
            $("#<%=TextBox3.ClientID %>").keyup(
            function changefocus() {
                if ($("#<%=TextBox3.ClientID %>").val().length >= $("#<%=TextBox3.ClientID %>").attr('maxlength'))
                    $("#<%=TextBox4.ClientID %>").focus();
            });
        });
    </script>

</head>
<body>

   <form id="form1" runat="server">

    <asp:TextBox ID="TextBox1" runat="server" MaxLength="3"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server" MaxLength="3"></asp:TextBox>
    <asp:TextBox ID="TextBox3" runat="server" MaxLength="4"></asp:TextBox>
    <asp:TextBox ID="TextBox4" runat="server" MaxLength="4"></asp:TextBox>

    </form>

</body>
</html>

 Insert multiple data in database from gridview using Select All button

<asp:GridView ID="grdSelectEmp" runat="server" AllowPaging="false"
                                            AutoGenerateColumns="False" CellPadding="4" EmptyDataText="No Record Exists"
                                            ForeColor="#333333" GridLines="Both" Height="450px" Width="100%"
                                            onrowcommand="grdSelectEmp_RowCommand">
                                            <AlternatingRowStyle BackColor="White" HorizontalAlign="Left" />
                                            <Columns>
                                                <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Employee ID" ItemStyle-HorizontalAlign="Left">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblEmpId" runat="server" ForeColor="#333333" Text='<%# Eval("EmpCode")%>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Employee Name" ItemStyle-HorizontalAlign="Left">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblEmpName" runat="server" ForeColor="#333333" Text='<%# Eval("EmapName")%>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Designation" ItemStyle-HorizontalAlign="Left">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblDesignation" runat="server" ForeColor="#333333" Text='<%# Eval("DesignationName")%>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                                                    <HeaderTemplate>
                                                        <asp:Button ID="btnSelectAll" runat="server" CommandName="SelectAll_Clicked" Text="Select All" Width="80px" />
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:CheckBox ID="chkEmp" runat="server" AutoPostBack="true" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                            <EditRowStyle />
                                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
                                                HorizontalAlign="Left" />
                                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                            <RowStyle BackColor="#EFF3FB" HorizontalAlign="Left" />
                                        </asp:GridView>

.cs Code

 protected void btnSendNotification_Click(object sender, EventArgs e)
        {
            try
            {
                lblError.Visible = false;
                foreach (GridViewRow grdRow in grdSelectEmp.Rows)
                {
                    if (((CheckBox)grdRow.FindControl("chkEmp")).Checked == true)
                    {
                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
                        conn.Open();
                        SqlCommand cmd = new SqlCommand("proc_MeetingNotify", conn);
                        cmd.Parameters.AddWithValue("@Title", txtTitle.Text.Trim());
                        cmd.Parameters.AddWithValue("@MeetingDate", txtDate.Text.Trim());
                        cmd.Parameters.AddWithValue("@Location", txtLocation.Text.Trim());
                        cmd.Parameters.AddWithValue("@MeetingTime", txtTime.Text.Trim());
                        cmd.Parameters.AddWithValue("@EmpID", ((Label)grdRow.FindControl("lblEmpId")).Text.Trim());
                        cmd.Parameters.AddWithValue("@BranchID", ddlBranch.SelectedItem.Value);
                        cmd.Parameters.AddWithValue("@DepartmentID", ddlDepartment.SelectedItem.Value);
                        cmd.Parameters.AddWithValue("@EnteredBy", ViewState["user"].ToString());
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        conn.Dispose();                      
                    }

                }
                lblError.Visible = true;
                lblError.Text = "Notofication Has Send";
                Insert_Notification();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }