Tuesday, November 26, 2013

Show Progress bar during data binding in Gridview

Show Progress bar during data binding in Gridview
Hello friends,
Today I will be explain , how can we use progress bar in our application, when we are binding data to gridview or editing information to gridview or datalaist. Generally when we are bind are editing data it will take some time for display information or update data. In that situation a normal user can’t determine what’s going on. In this case when we are using progress bar, then it will be display information regarding progress of operation.
So there, now I will be explaining all steps how we can we use progress bar to our website using gridview.
Step 1: Create database and table using this script
Create database testdata
CREATE TABLE [dbo].[Accordian](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [name] [nvarchar](50) NULL,
      [address] [nvarchar](500) NULL,
      [phno] [nvarchar](13) NULL,
      [emailid] [nvarchar](50) NULL,
 CONSTRAINT [PK_Centerdetail] PRIMARY KEY CLUSTERED
(
      [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Step 2: Now Start your project and add new Project
Step 3: Now Add a new page with Name gridview.aspx.
Step 4: Add reference for Ajaxtoolkit to your project in bin folder.
Step 5: Now write this code for drag an updatepanel to your toolbox and add an updateprogressbar to toolbox and add a gridview. Code will be like this.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview.aspx.cs" Inherits="gridview" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <div><asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate><div style=" left:30%; top:20%; position:absolute;background-color: Gray;
filter: alpha(opacity=80);
opacity: 1;">
     <asp:UpdateProgress ID="uprNavigation"  runat="server" AssociatedUpdatePanelID="UpdatePanel1" >
                <ProgressTemplate >
                    <asp:Image ToolTip="Loading" ID="imgLoader" runat="server" ImageUrl="~/preogressbar.gif"/>
                </ProgressTemplate>
            </asp:UpdateProgress></div>
    <asp:GridView ID="grdta" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None" Width="750px" AutoGenerateColumns="false"
            onrowcancelingedit="grdta_RowCancelingEdit" onrowediting="grdta_RowEditing"
            onrowupdating="grdta_RowUpdating"><Columns>
            <asp:TemplateField><ItemTemplate><asp:Label ID="lblname" Text='<%#Eval("name") %>' runat="server"></asp:Label></ItemTemplate><EditItemTemplate><asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></EditItemTemplate></asp:TemplateField>
             <asp:TemplateField><ItemTemplate><asp:Label ID="lblemil" Text='<%#Eval("emailid") %>' runat="server"></asp:Label></ItemTemplate><EditItemTemplate><asp:TextBox ID="txtemail" runat="server" Text='<%#Eval("emailid") %>'></asp:TextBox></EditItemTemplate></asp:TemplateField>
            <asp:TemplateField><ItemTemplate><asp:LinkButton ID="lnkedit" CommandName="Edit" runat="server" Text="Edit"></asp:LinkButton></ItemTemplate></asp:TemplateField>
            </Columns>
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView></ContentTemplate></asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


Step 6: This code for bind gridview.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class gridview : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=abc; Initial Catalog=testdata; User Id=santosh; pwd=123; ");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
        }
    }
    //
    public void binddata()
    {
        con.Open();
SqlCommand cmd = new SqlCommand("Select * from dbo.Accordian", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
grdta.DataSource = ds;
grdta.DataBind();
    con.Close();
}
    }
    protected void grdta_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdta.EditIndex = -1;
        binddata();
    }
    protected void grdta_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdta.EditIndex = e.NewEditIndex;
        binddata();

    }
    protected void grdta_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
}

This is the snapshot for this project is.



You can download source code from theredownload

Monday, October 14, 2013

Mail Merge - Using Google Spreadsheet and Gmail

Inserting the Mail Merge function to spreadsheet

1. Open google drive
2. Create-->spreadsheet
3. Tools-->Script Gallery
4. Type "Yet Another" in search bar
5. Install "Yet another mail merge"
6. Authorise
7. Maximise box until you can see "accept" click this
8. Close down box where you installed "Yet another mail merge"
9. The mail merge option is now available on this google spreadsheet.

Populate spreadsheet with data and draft email

1. Create the columns - e.g. firstname, lastname, groupname, email address
2. Add in the data
3. Draft email in gmail and save - remember to give it a subject line but leave the "To" field blank.
e.g.

Dear $%firstname%

Blah Blah, please delete $%groupname% blah blah

Many thanks

Dina

Signature

note: if you taking a field from a spreadsheet always use "$%texthere%" (you can make this bold, italic etc if you prefer)

4. Navigate back to spreadsheet and click "Mail Merge"
5. Standard Merge
6. Which column contains the recipients? Select column name
7. Select the draft email that you have saved in gmail
8. Send mails
9. The spreadsheet will say "done" for each person, when completed.
10. Check your sent items to confirm

You might like to send a couple of test emails to the team before you populate with actual data for mass mail merge.

Post back and let me know if these instructions where easy to follow and if you where successful.

Saturday, September 14, 2013

Dynamic Multi Drop down menu bar using XSLT, Sql Server and Asp.net


Dear friends,
 Today , I will be explain to you that how can we create dynamic menu bar with category and subcategory using C# asp.net ,XSLT and SQL Server. Now imaging to you explain all steps one by one.
Step 1:  First of all we design our database.
Database:
create database menubar
Now i will be create tables.
[1]. CREATE TABLE [dbo].[categories](
      [CatId] [int] IDENTITY(1,1) NOT NULL,
      [Category] [nvarchar](50) NULL,
      [createdate] [datetime] NULL,
      [status] [bit] NULL,
 CONSTRAINT [PK_categories] PRIMARY KEY CLUSTERED
(
      [CatId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

[2].
CREATE TABLE [dbo].[subcategory](
      [subatid] [int] IDENTITY(1,1) NOT NULL,
      [subcategoryname] [nvarchar](50) NULL,
      [catid] [int] NULL,
      [status] [bit] NULL,
      [createdate] [datetime] NULL
) ON [PRIMARY]


Step 2: Now we create a new website using Visual studio, and add a page with the name of menu.aspx.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="menubar.aspx.cs" Inherits="menubar" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>
Step 3: Now add css file to header of menuaspx page, that will be define structure of header.
<style type="text/css">
<!--
@import url("css/Style.css");
@import url("Styles/nav.css");
body {
       margin-left: 0px;
       margin-top: 0px;
       margin-right: 0px;
       margin-bottom: 0px;
       text-align: center;
       background-color: #FFF;
}html {
scrollbar-face-color: #E7EBEE;
scrollbar-shadow-color: #999999;
scrollbar-highlight-color: #FFFFFF;
scrollbar-3dlight-color:#999999;
scrollbar-darkshadow-color: #F4F4F4;
scrollbar-track-color:#999;
scrollbar-arrow-color: #410539;
}
a:link {
       font-family:Arial, Helvetica, sans-serif;
       font-size:11px;
       color: #000;
       text-decoration: none;    
}
a:visited {
       font-family:Arial, Helvetica, sans-serif;
       font-size:11px;
       color: #000;
       text-decoration: none;    
}
a:hover {
       font-family:Arial, Helvetica, sans-serif;
       font-size:11px;
       color: #333;
       text-decoration: none;    
}
a:active {
       font-family:Arial, Helvetica, sans-serif;
       font-size:11px;
       color: #000;
       text-decoration: none;    
}
    -->
</style>


This is the snapshots
Step 4: now we will be use a Literl control on menu.aspx page. For bind the menu information to this literal control and we add all this code to our page menu.aspx.

 <div>
   


<table  border="0" cellspacing="0" cellpadding="0" width="1005px">
        <tr>
          <td align="left" valign="bottom" ><div style="position:relative; z-index:100 ; background:url(../images/menu_bg.jpg); background-repeat:repeat-x; text-align:left; padding-top:5px; width:1005px;"><ul class="pureCssMenu">  
                 
      <li class="pureCssMenui0"><a class="pureCssMenui0" href="Default.aspx" title="Home"><span>Home</span>
        <![if gt IE 6]>
        </a>
      
      </li>
      <li class="pureCssMenui0" style="color:White;">|</li>
      <li class="pureCssMenui0"><a class="pureCssMenui0" href="aboutus.aspx" title="About us" ><span>About Us</span>
        <![if gt IE 6]>
        </a>
      
      </li>
       <li class="pureCssMenui0"><a class="pureCssMenui0" title="Products" href="#"><span>Products</span>
        <![if gt IE 6]>
        </a>
      
   <asp:literal runat="server" ID="Literal1"></asp:literal>
       

       
      
      </li>
      <li class="pureCssMenui0"><a class="pureCssMenui0" href="brand.aspx" title="Brnds"><span>Brands</span>
        <![if gt IE 6]>
        </a>
       
      </li>
       <li class="pureCssMenui0"><a class="pureCssMenui0" href="client.aspx" title="Brnds"><span>Clients</span>
        <![if gt IE 6]>
        </a>
       
      </li>
     
       
    </ul></div></td>
        </tr>
      </table>
    </div>

Step 3: Now create a folder XsltFormatFolder name. Right click on this folder andd  click on add new item. Add a xslt file and rename TransformXSLT.xsl. your xslt file will be look like this.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
> 
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Step 4: Now go on codebehind menu.cs and write this code.
using System;
using System.Collections.Generic;

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

using System.Xml;
using System.IO;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Text;
using System.Data.SqlClient;

public partial class menubar : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

  
    Hashtable hst = new Hashtable();
    DataSet dst = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Literal1.Text = ExecuteXSLTransformation();
        }
    }
    protected void clk_CushionCover_Click(object sender, EventArgs e)
    {

        //Redirect("Cushion Covers");
    }
    //........................this is the method for bind the prodecut menu bar....................by santoh kushwaha


    public string ExecuteXSLTransformation()
    {
        string HtmlTags, XsltPath;
        MemoryStream DataStream = default(MemoryStream);
        StreamReader streamReader = default(StreamReader);

        try
        {
            //Path of XSLT file
            XsltPath = HttpContext.Current.Server.MapPath("XsltFormatFolder/TransformXSLT.xsl");

            //Encode all Xml format string to bytes
            byte[] bytes = Encoding.ASCII.GetBytes(GenerateXmlFormat());

            DataStream = new MemoryStream(bytes);

            //Create Xmlreader from memory stream

            XmlReader reader = XmlReader.Create(DataStream);

            // Load the XML
            XPathDocument document = new XPathDocument(reader);

            XslCompiledTransform XsltFormat = new XslCompiledTransform();

            // Load the style sheet.
            XsltFormat.Load(XsltPath);

            DataStream = new MemoryStream();

            XmlTextWriter writer = new XmlTextWriter(DataStream, Encoding.ASCII);

            //Apply transformation from xml format to html format and save it in xmltextwriter
            XsltFormat.Transform(document, writer);

            streamReader = new StreamReader(DataStream);

            DataStream.Position = 0;

            HtmlTags = streamReader.ReadToEnd();

            return HtmlTags;
        }
        catch (Exception ex)
        {
            string ErrorMsg = ex.Message;
            return ErrorMsg;
        }
        finally
        {
            //Release the resources

            // streamReader.Close();

            //DataStream.Close();
        }

    }

    public string GenerateXmlFormat()
    {
        string SqlCommand;
        DataSet DbMenu;
        DataRelation relation;

        {

            SqlCommand = "Select MenuID, Name,Url, ParentID from Menu";

            DbMenu = new DataSet();

            SqlDataAdapter Adapter = new SqlDataAdapter(SqlCommand, con);

            Adapter.Fill(DbMenu);

            Adapter.Dispose();
        }

        DbMenu.DataSetName = "Menus";

        DbMenu.Tables[0].TableName = "Menu";

        //create Relation Parent and Child
        relation = new DataRelation("ParentChild", DbMenu.Tables["Menu"].Columns["MenuID"], DbMenu.Tables["Menu"].Columns["ParentID"], true);

        relation.Nested = true;

        DbMenu.Relations.Add(relation);

        return DbMenu.GetXml();
    }

    protected void clk_BedSheet_Click(object sender, EventArgs e)
    {

        //Redirect("Bed Sheets");
    }
    protected void clk_Towels_Click(object sender, EventArgs e)
    {

        //Redirect("Towels");
    }
    protected void clk_Upholstry_Click(object sender, EventArgs e)
    {

        //Redirect("Upholstry");
    }

    protected void clk_Curtains_Click(object sender, EventArgs e)
    {

        //Redirect("Curtains");
    }

    protected void FirstNewArrival_Click(object sender, EventArgs e)
    {

        //Redirect("New Arrivals");
    }
    protected void SecondNewArrival_Click(object sender, EventArgs e)
    {

        // Redirect("New Arrivals");
    }

    protected void clk_BedSpreads_Click(object sender, EventArgs e)
    {

        //Redirect("Bed Spreads");
    }

    protected void clk_BathMats_Click(object sender, EventArgs e)
    {

        //Redirect("Bath Mats");
    }
    protected void clk_Accessories_Click(object sender, EventArgs e)
    {

        //Redirect("Accessories");
    }
}

Step 5: now we need to map xslt file to our database table column name, which we wnat to display like this.
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Find the root node called Menus then convert it to <UL> </UL> HTMLTags
       and call MenuListing for its children -->
  <xsl:template match="/Menus">
    <ul>
      <xsl:attribute name="class">
        <xsl:text>sf-menu</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:text>nav</xsl:text>
      </xsl:attribute>
      <xsl:call-template name="MenuListing" />
    </ul>
  </xsl:template>

  <!-- Allow for recusive child node processing -->
  <xsl:template name="MenuListing">
    <xsl:apply-templates select="Menu" />
  </xsl:template>

  <xsl:template match="Menu">
    <li>
      <a>
        <!-- Convert Menu child elements to <li> <a> html tags  and attributes inside a tag -->
        <xsl:attribute name="href">
          mainproduct.aspx?CatId=<xsl:value-of select="ParentID"/><![CDATA[&]]>CName=<xsl:value-of select="Name"/>
        </xsl:attribute>
        <xsl:value-of select="Name"/>
      </a>
      <!-- Recursively call MenuListing for child menu nodes -->

      <xsl:if test="count(Menu) > 0">
        <ul>
          <xsl:call-template name="MenuListing" />
        </ul>
      </xsl:if>
    </li>
  </xsl:template>
</xsl:stylesheet>




This is the article about how can we bind a multi dropdown menubar using xslt and sql server. You can download full source code from here.