Saturday, January 30, 2010

Open PDF file in Asp.net using Vb.net on button click event

This example used the asp.net with vb.net coding.

Here i am upload the PDF file path which is already stored on server side hard drive and this file path store in string variable sPath and now i was click the one button from Asp.net page then the particular PDF is open.

Code is below:
Placed following code  in button click event....

Dim client As New System.Net.WebClient()
Dim buffer As [Byte]() = client.DownloadData(sPath)

If buffer IsNot Nothing Then
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
End If

Friday, January 29, 2010

How to retrieve image in gridview from the sql table (which is store as path in the table ) in Asp.net



You use an ImageField to display an image stored on the server’s hard drive. You can’t use
an ImageField to display images stored in a database table.

Following code shows how you can use the ImageField when creating a simple photo gallery

Using an ImageField with the GridView control.

Code For Above Diagram and check example code:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">

  protected void frmPhoto_ItemInserting(object sender, FormViewInsertEventArgs e)
  {
        // Get the FileUpload control
        FileUpload upPhoto = (FileUpload)frmPhoto.FindControl("upPhoto");
        srcImages.InsertParameters["FileName"].DefaultValue = upPhoto.FileName;        

        string savePath = MapPath("~/Photos/" + upPhoto.FileName);
        // Save contents to file system
        upPhoto.SaveAs(savePath);
    }

    protected void frmPhoto_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {

    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show ImageField</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
    <asp:GridView
        id="grdImages"
        DataSourceID="srcImages"
        AutoGenerateColumns="false"
        ShowHeader="false"
        Runat="server">
        <Columns>
        <asp:ImageField
            DataImageUrlField="FileName"
            DataImageUrlFormatString="~/Photos/{0}"
            DataAlternateTextField="AltText"
            ControlStyle-Width="200px" />
        </Columns>
    </asp:GridView>
  
    <asp:SqlDataSource
        id="srcImages"
        ConnectionString="<%$ ConnectionStrings:Photos %>"
        SelectCommand="SELECT FileName, AltText FROM Photos"
        InsertCommand="INSERT Photos (FileName, AltText)
            VALUES (@FileName, @AltText)"
        Runat="server">
        <InsertParameters>
            <asp:Parameter Name="FileName" />
        </InsertParameters>
    </asp:SqlDataSource>  
          
    <hr />
    <asp:FormView
        id="frmPhoto"
        DefaultMode="Insert"
        DataSourceID="srcImages"
        OnItemInserting="frmPhoto_ItemInserting"
        Runat="server" onpageindexchanging="frmPhoto_PageIndexChanging">
        <InsertItemTemplate>
        <h1>Add Photo</h1>
        <asp:Label
            id="lblPhoto"
            Text="Photo:"
            AssociatedControlID="upPhoto"
            Runat="server" />
        <br />
        <asp:FileUpload
            id="upPhoto"            Runat="server" />
        <br />
        <asp:Label
            id="lblAltText"
            Text="Alternate Text:"
            AssociatedControlID="txtAltText"
            Runat="server" />
        <br />
        <asp:TextBox
            id="txtAltText"
            Text='<%# Bind("AltText") %>'
            Columns="50"
            Runat="server" />
        <br />
        <asp:Button
            id="btnInsert"
            Text="Add New Photo"
            CommandName="Insert"
            Runat="server" />          
        </InsertItemTemplate>
    </asp:FormView>
  
    </div>
    </form>
</body>
</html>
-----------------------------------------------------------------------------------


The GridView in above example contains an ImageField that looks like this:
<asp:ImageField
DataImageUrlField=”FileName”
DataImageUrlFormatString=”~/Photos/{0}”
DataAlternateTextField=”AltText”
ControlStyle-Width=”200px” />

The DataImageUrlField property contains the name of a field from the data source that
represents the path to an image on the server hard drive. The DataImageUrlFormatString
enables you to format this path. Finally, the DataAlternateTextField enables you to
specify the value of the alt attribute used by the <img> tag.


An ImageField supports the following properties:


. AlternateText—Enables you to specify fixed alternate text.
. DataAlternateTextField—Enables you to specify a field that represents the
alternate text.
. DataAlternateTextFormatString—Enables you to format the alternate text.
. DataImageUrlField—Enables you to specify a field that represents the image path.
. DataImageUrlFormatString—Enables you to format the image path.
. NullImageUrl—Enables you to specify an alternate image when the
DataImageUrlField is Nothing (null).