Wednesday, April 18, 2012

how to export gridview data to PDF using asp.net or Export gridview data to PDF using asp.net

Introduction:

Here I will explain how to export gridview data to PDF using asp.net.



Description:

In my previous articles I explained clearly how to export gridview data to excel or word and how to export gridview data to CSV file . Now I will explain how to export gridview data to PDF using asp.net. In asp.net we don’t have direct feature to export gridview data to PDF for that reason here I am using third party library ITextSharp reference. The dll is available here ITextSharp first download dll from this site after that create one new website in visual studio and add ITextsharp dll reference to newly created website after that design aspx page like this 

 <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td align="right">
<asp:ImageButton ID="btnPDF" runat="server" ImageUrl="~/PDF.jpg" Width="32px"
Height="32px" onclick="btnPDF_Click"/>
</td>
</tr>
<tr>
<td>
<asp:GridView runat="server" ID="gvdetails" DataSourceID="dsdetails"  AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from UserInformation"/>
</div>
</form>
</body>
</html>
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings>
 
Now in code behind add these references
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
After that write the following code in code behind
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void btnPDF_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.RenderControl(hw);
gvdetails.HeaderRow.Style.Add("width", "15%");
gvdetails.HeaderRow.Style.Add("font-size", "10px");
gvdetails.Style.Add("text-decoration", "none");
gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdetails.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
  If you observe above code I added one function that is VerifyRenderingInServerForm this function is used to avoid the error like “control must be placed in inside of form tag”. If we set VerifyRenderingInServerForm function then compiler will think that controls rendered before exporting and our functionality will work perfectly
http://www.aspdotnet-suresh.com/2011/04/how-to-export-gridview-data-to-csv-file.html
  

Sunday, April 15, 2012

Islamins sites

http://www.masnoonazkars.com/
http://www.masnoonazkars.com/

Sunday, April 1, 2012

Display Alert msg and method......

 public static void Show(string message)
    {
        // Cleans the message to allow single quotation marks
        string cleanMessage = message.Replace("'", "\\'");
        string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');window.location='UserSearch.aspx';</script>";

        // Gets the executing web page
        Page page = HttpContext.Current.CurrentHandler as Page;

        // Checks if the handler is a Page and that the script isn't allready on the Page
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(typeof(UserManagement_Controls_ctrl_UserCreate), "alert", script);
           
        }
    }



 UserManagement_Controls_ctrl_UserCreate.Show(error.gridlablerError());

Friday, March 30, 2012

.NET Sample Projects with Source Code

Academic Projects for Students
  • Browse through projects and download source code

  • Register as a project trainee under any project

  • Post questions and get answers from experienced project guides

  • Develop the project yourself, submit and get project completion certificate from dotnetspider.


General .NET Projects

please visit:

http://www.dotnetspider.com/projects/index.aspx?


http://all-free-download.com/free-website-templates/business_553.html

Display Microsoft Excel File in ASP.NET GridView


Microsoft Excel is still very powerful and widely used tool in almost all the companies. Many people use it extensively to store and analyze tabular data without learning a relational database management system such as SQL Server or Oracle. If you are creating ASP.NET website, there are chances that the data is made available to you in an Excel file and reading and displaying Excel data in ASP.NET page can make your applications more powerful for analysis. In this tutorial I will show you how to read Excel file and displaying data in ASP.NET GridView control. 
For the purpose of this tutorial, I have created a sample Excel sheet “SearchEngines.xls” that holds the records of different search engines. Make sure you have saved this file in your ASP.NET website special folder named App_Data so that we can interact with it from ASP.NET page without worrying about its absolute path.


Sample Excel File

Next I have created ASP.NET page which is very straight forward with only one Button and GridView control on it. Following is the HTML source for the (.aspx) file.
<form id="form1" runat="server">
   <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Display Excel Data" />
   <br>
   <br>
   <asp:gridview id="GridView1" runat="server" cellpadding="6" gridlines="None"
        bordercolor="#336699" borderstyle="Solid" borderwidth="1px">
       
        <headerstyle backcolor="#336699" font-bold="True" forecolor="White" />
   </asp:gridview>
</form>

In the C# code of the button Click event I will connect and read Excel file and then I will bind the data to GridView control. To connect Excel file you need to use Microsoft Jet OLEDB data provider as you can see from the following connection string. The path of the excel file is provided by using ASP.NET DataDirectory feature which points to the App_Data folder in your Website.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|SearchEngines.xls;Extended Properties='Excel 8.0;HDR=Yes;'
You can make sure of the OLEDB data provider to treat the Excel file as a data source. Where Excel Workbook can be assumed as the Database and the sheets in the Workbook are considered as tables. In this way, you can fire SQL queries to the Excel file to retrieve data from the sheets. Following code shows you how to connect and read data from Excel using OleDbDataAdapter class available in System.Data.OleDb namespace.
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|SearchEngines.xls;Extended Properties='Excel 8.0;HDR=Yes;'";

string query = "SELECT * FROM [Sheet1$]";

DataSet excelDataSet = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(query, strConn);

da.Fill(excelDataSet);

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

The output of the above code can be shown below.

Excel Data Displaying in GridView

I hope this tutorial will help many of you specially those who are new in ASP.NET and want to connect Excel from ASP.NET page. 

Wednesday, March 28, 2012

Lots of Free Skins for DotNetNuke

Each week we're releasing more free skins into our collection.  All of these skins are available at no charge exclusively to DNNCreative Subscribers.
for details please visit:
http://www.dnncreative.com/TrainingTutorials/SkinningTutorials/14FreeSkinsforDotNetNuke/tabid/640/Default.aspx