welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, June 6, 2011

How to retrieve uploaded images from the database using


In Previous article i explained about How to upload image to database and How to validate that image,For your refernce i mention the article url below
Now i am going to explain about to retrieve the uploaded images from the database and View through Data Grid View..

Before in SQL you have to create a table like this..
CREATE TABLE [dbo].[upload](
 [fileid] [int] IDENTITY(1,1) NOT NULL,
 [ImageName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CS_AS NULL,
 [Image] [image] NULL


 If FileUpload1.HasFile Then

                Dim intlength As Integer
                Dim bytes1() As Byte = pstFile(FileUpload1) 'Function Used to read bytes from the upload image
                ' Create SQL Connection 

                Dim con As New SqlConnection()
                con.ConnectionString = ConfigurationManager.ConnectionStrings("phone").ConnectionString

                intlength = Convert.ToInt32(FileUpload1.PostedFile.InputStream.Length)
                Dim cmd As New SqlCommand()
                cmd.CommandText = "INSERT INTO upload(ImageName,Image) VALUES (@ImageName,@Image)"
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                Dim ImageName As New SqlParameter("@ImageName", SqlDbType.VarChar, 50)
                ImageName.Value = TextBox1.Text.ToString

                Dim imagelenth As New SqlParameter("@Image", SqlDbType.Image)
                imagelenth.Value = bytes1
                Dim result As Integer = cmd.ExecuteNonQuery()

                 loadgrid() 'Used To store image value in grid

                Label1.Text = "please select the file to uplaod"
            End If

 'Function Used to read bytes from the upload image

  Function pstFile(ByVal passfile As FileUpload) As Byte()
        Dim fs As Stream
        Dim bytes1 As Byte()
        Dim postfile As Byte()
        fs = passfile.PostedFile.InputStream
        Dim br1 As New BinaryReader(fs)
        bytes1 = br1.ReadBytes(passfile.FileContent.Length)
        postfile = bytes1
        Return postfile
    End Function

 'Function Used to store image value in gridview

  Sub loadgrid()
        Dim fselect As String
        Dim adp As New SqlDataAdapter
        Dim ds As New DataSet
        fselect = "select fileid,ImageName from upload"
        Dim con As New SqlConnection()

        con.ConnectionString = ConfigurationManager.ConnectionStrings("con").ConnectionString
        Dim cmd As New SqlCommand()
        cmd = New SqlCommand(fselect, con)
        adp = New SqlDataAdapter(cmd)
        ds = New DataSet
        dt = ds.Tables(0)
        Session("dataimage") = dt
        imggrid.DataSource = dt
    End Sub

'client Side Coding for create grid view

    <form id="form1" runat="server">
     <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <br />
      Enter the File name to Upload  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
       <br />
        click here to upload the file  <asp:Button ID="Button1" runat="server" 
            Text="submit" style="height: 26px" />
    <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
         <table cellpadding ="0" cellspacing ="0"  width: "100%"> 
        <td height="300">
        <asp:GridView ID="imggrid" runat="server" AutoGenerateColumns ="false" 
                Width="97%"  >
        <Columns >
        <asp:TemplateField HeaderText ="Name">
        <ItemTemplate >
        <asp:TemplateField HeaderText ="Image">
        <ItemTemplate >
        <asp:Image ID="Image1" runat="server" ImageUrl ="Image.aspx?id=0" /> 'This image File is called form the page image.aspx

For view an image we have to create one another page like image.aspx, and write the following coding in the server side.....
image.aspx  page is called from the Grid View..
By the same way you can get the imagename from the query string ,By using image id get the image from the database and show in grid view.........

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim con As New SqlConnection
        Dim i As Integer
        i = CInt(Request.QueryString("ID").ToString)

        con.ConnectionString = ConfigurationManager.ConnectionStrings("con").ConnectionString
        Dim sqlcmd As New SqlCommand
        Dim sqldr As SqlDataReader

        sqlcmd = New SqlCommand("select image from upload where ='" & i & "'", con)
        sqldr = sqlcmd.ExecuteReader()

        If sqldr.HasRows Then
        End If

    End Sub

    End Sub
I hope this one really helpful to you........


Post a Comment