Click here to Skip to main content
15,896,154 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
XML
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!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 id="Head1" runat="server">
    <title>Save Retrieve Images</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="lblEmpName" runat="server" Text="Employee Name"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtEName" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="lblImage" runat="server" Text="Employee Image"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
        <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" />



    </div>
    </form>
</body>
</html>

My Codes on VB.net
VB
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
        Try
            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")

            connection.Open()
            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
        Catch
            lblResult.Text = "There was an error"
        Finally
            connection.Close()
        End Try

    End Sub
End Class

My Codes on handler.ashx
VB
<%@ 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"))
        Else
            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)
        Loop
        'context.Response.BinaryWrite(buffer);
    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)
        connection.Open()
        Dim img As Object = cmd.ExecuteScalar()
        Try
            Return New MemoryStream(CType(img, Byte()))
        Catch
            Return Nothing
        Finally
            connection.Close()
        End Try
    End Function

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


End Class


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

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
 
Comments
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 vb.net 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
9810353598
 
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