Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello programmers, got a problem here. As you see i want to create a uploading/viewing of pictures in database. my problem is that it can only be stored on the database but cannot be viewed. I have (I think) the codes to view it but still it isnt working.. Plse help me
My codes on aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head id="Head1" runat="server">
    <title>Save Retrieve Images</title>
    <form id="form1" runat="server">

        <asp:Label ID="lblEmpName" runat="server" Text="Employee Name"></asp:Label>
        <asp:TextBox ID="txtEName" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="lblImage" runat="server" Text="Employee Image"></asp:Label>
        <asp:FileUpload ID="imgUpload" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
            Text="Submit" />

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp        <asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label>
    <br />
    <hr />

   <asp:Image ID="Image1" style="width:200px" Runat="server" />


My Codes on
Imports System.Data.SqlClient

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim connection As SqlConnection = Nothing
            Dim img As FileUpload = CType(imgUpload, FileUpload)
            Dim imgByte As Byte() = Nothing
            If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
                Dim File As HttpPostedFile = imgUpload.PostedFile
                imgByte = New Byte(File.ContentLength - 1) {}
                File.InputStream.Read(imgByte, 0, File.ContentLength)
            End If

            connection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")

            Dim sql As String = "INSERT INTO Employee(empname,empimg) VALUES(@enm, @eimg) SELECT @@IDENTITY"
            Dim cmd As SqlCommand = New SqlCommand(sql, connection)
            cmd.Parameters.AddWithValue("@enm", txtEName.Text.Trim())
            cmd.Parameters.AddWithValue("@eimg", imgByte)
            Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar())
            lblResult.Text = String.Format("Employee ID is {0}", id)
            Image1.ImageUrl = "~/Handler.ashx?id=" & id
            lblResult.Text = "There was an error"
        End Try

    End Sub
End Class

My Codes on handler.ashx
<%@ WebHandler Language="VB" Class="Handler"  %>

Imports System
Imports System.Configuration
Imports System.Web
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Public Class ShowImage
    Implements IHttpHandler
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim empno As Int32
        If Not context.Request.QueryString("id") Is Nothing Then
            empno = Convert.ToInt32(context.Request.QueryString("id"))
            Throw New ArgumentException("No parameter specified")
        End If

        context.Response.ContentType = "image/jpeg"
        Dim strm As Stream = ShowEmpImage(empno)
        Dim buffer As Byte() = New Byte(4095) {}
        Dim byteSeq As Integer = strm.Read(buffer, 0, 4096)

        Do While byteSeq > 0
            context.Response.OutputStream.Write(buffer, 0, byteSeq)
            byteSeq = strm.Read(buffer, 0, 4096)
    End Sub

    Public Function ShowEmpImage(ByVal empno As Integer) As Stream
       Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        Dim sql As String = "SELECT empimg FROM Employee WHERE empid = @ID"
        Dim cmd As SqlCommand = New SqlCommand(sql, connection)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@ID", empno)
        Dim img As Object = cmd.ExecuteScalar()
            Return New MemoryStream(CType(img, Byte()))
            Return Nothing
        End Try
    End Function

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Return False
        End Get
    End Property

End Class

plss help and more power programmers
Updated 5-Jul-11 17:09pm

Have you stepped through this code to make sure your handler is called and that it's getting the right info from the DB ?
Share this answer
janwel 5-Jul-11 23:02pm    
sir what do you mean? sorry cant understand fully english T_T
janwel 5-Jul-11 23:04pm    
sir I put the hander above tha catch on so im pretty sure i called it
janwel 5-Jul-11 23:07pm    
I think sir my problem is on hander as it is for retrieving pictures. but there is no error occuring (im new on this picture retriving)
Christian Graus 5-Jul-11 23:11pm    
OK, let me try again. If you have not set a breakpoint in your handler, and verified that it is being called, by letting a breakpoint run in your code, then you don't know if it's being called. If you've not stepped through the code, then you don't know what's happening when it is called. Until you know for sure, not by guessing, what is happening, it's hard to debug.
janwel 5-Jul-11 23:13pm    
sir how can i test the handler if it is working?
Dear ,
On insert time stored the value of Image .
* ImageByte value
* ImageType
* ImageLength

After Your Image image retrieval working.

Else if you store Imagetype so By default only JPEG image can access for database.

Rohit Kumar Singh
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900