Wednesday, September 7, 2011

Download PDF/Doc File from Web (asp.net,c#)

Client side Code
------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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>
    <asp:GridView ID="gvtest" runat="server" AutoGenerateColumns ="false">
    <Columns>
    <asp:TemplateField HeaderText="S.No.">
    <ItemTemplate >
    <%#Container.DataItemIndex+1 %>
    </ItemTemplate>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Link">
    <ItemTemplate >
    <asp:LinkButton ID="lnk" runat="server" Text="download" OnClick="lnk_Click" ToolTip='<%#Eval("LinkId")%>'></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
   
    </Columns>
    </asp:GridView>
    </div>
    </form>
</body>
</html>
-----Code Behind---------------------------------------------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            gvtest.DataSource = bindGrid();
            gvtest.DataBind();
        }
    }
    protected void lnk_Click(object sender, EventArgs e)
    {
        LinkButton lnkbutton = (LinkButton)sender;
        string id = lnkbutton.ToolTip.ToString();
        String fileName=id.ToString()+".pdf";
        FileInfo fl = new FileInfo(Server.MapPath("PDF/" +fileName ));
        if (fl.Exists)
        {
                    Response.ClearContent();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                    Response.AddHeader("Content-Length", fileName.Length.ToString());  
                    string CId = Request["__EVENTTARGET"];
                    Response.TransmitFile(fl.FullName);  
                    Response.End();  
                    //string path = fl.ToString();
                    //ScriptManager.RegisterStartupScript(Page, GetType(), "rs", "window.open('" + fl + "');", true);
        }
    }
    protected DataTable   bindGrid()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("LinkId");
        for (int i = 1; i <5; i++)
        {
            DataRow dr = dt.NewRow();
            dr["LinkId"] = i.ToString();
            dt.Rows.Add(dr);
            dt.AcceptChanges();
        }
        return dt;
    }
}



Transaction in C# with Sql-Server Code behind







using System;
using System.Data.SqlClient;

namespace CsharpWebApp
{
    public class TransactionDemo
    {
        public TransactionDemo()
        {

        }

        [STAThread]
        public static void Main()
        {
            Demo1();
        }

        private static void Demo1()
        {
            SqlConnection dbConn = new SqlConnection("sqlConnectionString");
            SqlTransaction transaction;

            dbConn.Open();
            transaction = dbConn.BeginTransaction();

            SqlCommand sqlDept = new SqlCommand("INSERT INTO EmployeeDepartment (EmpId, Department) VALUES (1,

'Department');", dbConn, transaction);
            SqlCommand sqlManager = new SqlCommand("INSERT INTO EmployeeManager (EmpId, Manager) VALUES (1,

'Department');", dbConn, transaction);

            try
            {
                sqlDept.ExecuteNonQuery();
                sqlManager.ExecuteNonQuery();
                transaction.Commit();
            }
            catch (SqlException sqlError)
            {
                transaction.Rollback();
            }
            finally
            {
                dbConn.Close();
                sqlDept.Dispose();
                sqlManager.Dispose();
                dbConn.Dispose();
            }
          
        }
    }

Implementing Service Locator (To Resolve Dependency)

using System; /// <summary> /// Summary description for Class1 /// </summary> public class serviceLocator {     public s...