This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Me And My Respected Teacher Mr Kamal Sheel Mishra

Mr. K.S. Mishra is HOD of Computer Science from SMS Varanasi where I have completed my MCA

Me And My Respected Teacher Mr Udayan Maiti

Mr. Udayan Maiti is a senior .Net Expert and has guided many professionals of multi national Companies(MNC)

Me And My Best Friend Mr Ravinder Goel

Mr. Ravinder Goel is a senior Software Engineer and now he is working Wipro Technology

Wednesday, 17 September 2014

How to Export GridView Data to Pdf in Asp.Net


How to Export GridView Data to Pdf in Asp.Net……………………….

Note :In this topic we will discuss how to export GridView Data to Pdf in Asp.Net

Step 1: First  we will go to  this link   http://sourceforge.net/projects/itextsharp/ and  Download here itextsharp.dll and follow this step …………………………….

Right Click on WebSte>Add References ->



After that we can see this itextsharp.dll in Bin Folder

--sql query………………………………………….

create database GridConcept
use GridConcept
create table Employee
(
SrNo int identity(1,1) primary key,
Name varchar(50),
Gender varchar(50),
Address varchar(50),
MobileNo varchar(50)
)
Default.aspx ………………………………………

<%@ 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></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
<font color="blue"><b><center>How to Export GridView Data to Pdf in Asp.Net</center></b></font> </td>
</tr>
<tr>
<td >
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</td>
</tr>
<tr>
<td >
<asp:Button ID="btnexportpdf" runat="server" onclick="btnexportpdf_Click"
Text="Export with Pdf without Header" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnwithHeader" runat="server" Text="with Header without Color"
onclick="btnwithHeader_Click" />
&nbsp;&nbsp;
<asp:Button ID="btnwithHeaderwithColor" runat="server"
Text="with Header with Color" onclick="btnwithHeaderwithColor_Click" />
</td></tr>
</table>
</div>
</form>
</body>
</html>

Code with image ………………………………………


Default.aspx.cs ………………………………………

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;
// using this namespace Export GridView to Pdf
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// CODE FOR BIND GRIDVIEW ON PAGELOAD ................
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=KUSHTIWARI-PC;Initial Catalog=GridConcept;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from Employee", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}

// Export GridView to Pdf without Header ................

protected void btnexportpdf_Click(object sender, EventArgs e)
{
PdfPTable pt = new PdfPTable(GridView1.HeaderRow.Cells.Count);
foreach (GridViewRow grv  in GridView1.Rows)
{
foreach(TableCell tc in grv.Cells )
{
PdfPCell pc = new PdfPCell(new Phrase(tc.Text));
pt.AddCell(pc);
}
}
Document pd = new Document(PageSize.A4, 15f, 15f, 15f, 15f);
PdfWriter.GetInstance(pd, Response.OutputStream);
pd.Open();
pd.Add(pt);
pd.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.pdf");
Response.Write(pd);
Response.Flush();
Response.End();
}

// Export GridView to pdf with Header but without GridView color................

protected void btnwithHeader_Click(object sender, EventArgs e)
{
PdfPTable pt = new PdfPTable(GridView1.HeaderRow.Cells.Count);
foreach (TableCell headercell in GridView1.HeaderRow.Cells)
{
PdfPCell pdfcell = new PdfPCell(new Phrase(headercell.Text));
pt.AddCell(pdfcell);
}
foreach (GridViewRow grv in GridView1.Rows)
{
foreach (TableCell tc in grv.Cells)
{
PdfPCell pc = new PdfPCell(new Phrase(tc.Text));
pt.AddCell(pc);
}
}

Document pd = new Document(PageSize.A4, 15f, 15f, 15f, 15f);
PdfWriter.GetInstance(pd, Response.OutputStream);
pd.Open();
pd.Add(pt);
pd.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.pdf");
Response.Write(pd);
Response.Flush();
Response.End();

}

// Export GridView to pdf with Header but with Color format................

protected void btnwithHeaderwithColor_Click(object sender, EventArgs e)
{
PdfPTable pt = new PdfPTable(GridView1.HeaderRow.Cells.Count);
foreach (TableCell headercell in GridView1.HeaderRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor);
PdfPCell pdfcell = new PdfPCell(new Phrase(headercell.Text,font));
pdfcell.BackgroundColor = new BaseColor(GridView1.HeaderStyle.BackColor);
pt.AddCell(pdfcell);
}

foreach (GridViewRow grv in GridView1.Rows)
{
foreach (TableCell tc in grv.Cells)
{
Font font = new Font();
font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor);
PdfPCell pc = new PdfPCell(new Phrase(tc.Text));
pc.BackgroundColor = new BaseColor(GridView1.RowStyle.BackColor);
pt.AddCell(pc);
}
}

Document pd = new Document(PageSize.A4, 15f, 15f, 15f, 15f);
PdfWriter.GetInstance(pd, Response.OutputStream);
pd.Open();
pd.Add(pt);
pd.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.pdf");
Response.Write(pd);
Response.Flush();
Response.End();
}

// apply style sheet on rowdatabound command................
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String mno = DataBinder.Eval(e.Row.DataItem, "MobileNo").ToString();
if (mno.Length < 10)
{
e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[4].Font.Bold = true;
e.Row.Cells[4].Font.Size = 15;
}
}
}
}

Result

Sunday, 14 September 2014

How to use CustomValidator in Asp.Net

How to use CustomValidator in Asp.Net
The CustomValidator control allows you to write a method to handle the validation of the value entered

Property /Description ...................
1.ControlToValidate The id of the control to validate
2.Display  The display behavior for the validation control. Legal values are:  
  1. None (the control is not displayed. Used to show the error message only in the ValidationSummary control)  
  2. Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.  
  3. Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
3.EnableClientScript  A Boolean value that specifies whether client-side validation is enabled or not
4.Enabled   A Boolean value that specifies whether the validation control is enabled or not
5.ErrorMessage   The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set
6.BackColor    background color of the CustomValidator control
7.ForeColor  The foreground color of the control
8.Runat   Specifies that the control is a server control. Must be set to "server"
9.Text  The message to display when validation fails
10.CssClass The message to display with cssclass property
11.Id:  A unique id for the control
12.IsValid   A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid
13.OnServerValidate Specifies the name of the server-side validation script function to be executed
For example :
Code for Default.aspx…………………….
<tr><td>Name</td><td>
<asp:Textbox id="text1" runat="server" text=""></asp:Textbox>
<asp:CustomValidator id="CustomValidator2" runat="server"
ControlToValidate = "text1"
ErrorMessage="Please enter Even No" onservervalidate="CustomValidator2_ServerValidate" >
</asp:CustomValidator>
</td>
</tr>

Code for Default.aspx.cs…………………….
protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs e)
{
//if (e.Value.Length == 8)
//    e.IsValid = true;
//else
//    e.IsValid = false;
int aa =Convert.ToInt32(e.Value);

if (aa % 2 == 0)
e.IsValid = true;
else
e.IsValid = false;

}

14.ClientValidationFunction Specifies the name of the client-side validation script function to be executed.
Note: The script must be in a language that the browser supports, such as VBScript or JScript
With VBScript, the function must be in the form:
Sub FunctionName (source, arguments)
With JScript, the function must be in the form:
function FunctionName (source, arguments)
For Example: 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<%--<script type="text/javascript">
function validateLength(oSrc, args) {
args.IsValid = (args.Value.length >= 8);
}
</script>--%>

<script type="text/javascript">
function validateLength(oSrc, args)
{
var v=args.Value;
args.IsValid = (v %2==0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Textbox id="text1" runat="server" text=""></asp:Textbox>
<asp:CustomValidator id="CustomValidator2" runat="server"
ControlToValidate = "text1"
ErrorMessage = "You must enter even no "
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>

Result: