locked
Show an default image if the correct image i snot available RRS feed

  • Question

  • User810354248 posted

    .aspx 

    <asp:Image ID="Image1" runat="server" Width="200px" Height="200px" ImageUrl='<%# "ImageHandler.ashx?ID=" + Convert.ToString(Eval("ID"))%>'/>

    imagehandler.ashx

    <%@ WebHandler Language="VB" Class="ImageHandler" %>
    
    Imports System
    Imports System.Web
    Imports System.Configuration
    Imports System.Data.SqlClient
    Imports System.Data
    
    Public Class ImageHandler : Implements IHttpHandler
        
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            Dim connectionString As String = ConfigurationManager.ConnectionStrings("baijuep").ConnectionString
            Dim conn As New SqlConnection(connectionString)
            Dim cmd As New SqlCommand()
            cmd.CommandText = "Select [Photo] from Images where ID =@ID"
            cmd.CommandType = CommandType.Text
            cmd.Connection = conn
    
            Dim ImageID As New SqlParameter("@ID", SqlDbType.BigInt)
            ImageID.Value = context.Request.QueryString("ID")
            cmd.Parameters.Add(ImageID)
            conn.Open()
            Dim dReader As SqlDataReader = cmd.ExecuteReader()
            dReader.Read()
            context.Response.BinaryWrite(DirectCast(dReader("Photo"), Byte()))
            dReader.Close()
            conn.Close()
        End Sub
     
        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
    End Class

    SQL datasource

    <asp:SqlDataSource ID="displaydb" runat="server" 
        ConnectionString="<%$ ConnectionStrings:baijuep %>" 
        SelectCommand="SELECT [Photo] FROM [Images] WHERE ([EmpNo] = @EmpNo)">
            <SelectParameters>
                <asp:QueryStringParameter Name="EmpNo" QueryStringField="EMPNO" Type="String" />
            </SelectParameters>
        
    </asp:SqlDataSource> 

    the above code works for me to display images from data base as per selected employee number.

    If the image i snot available then i want to show a default image in stead of employees image.

    Thursday, November 8, 2018 12:03 PM

All replies

  • User283571144 posted

    Hi Baiju EP,

    According to your description, I found you get the image url from the ashx ProcessRequest method.

    The ashx ProcessRequest method will get image url according to the "ID".

    Do you mean you want to return the default image url if we couldn't get the image url according to the "ID"?

    If this is your requirement, I suggest you could create if else logic in the image handler ashx.

    More details, you could refer to below codes:

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            Dim connectionString As String = ConfigurationManager.ConnectionStrings("baijuep").ConnectionString
            Dim conn As New SqlConnection(connectionString)
            Dim cmd As New SqlCommand()
            cmd.CommandText = "Select [Photo] from Images where ID =@ID"
            cmd.CommandType = CommandType.Text
            cmd.Connection = conn
     
            Dim ImageID As New SqlParameter("@ID", SqlDbType.BigInt)
            ImageID.Value = context.Request.QueryString("ID")
            cmd.Parameters.Add(ImageID)
            conn.Open()
            Dim dReader As SqlDataReader = cmd.ExecuteReader()
            If dReader.Read() Then
         if dReader("Photo").GetType.Name.Equals("DBNull") Then
              context.Response.BinaryWrite('default page',Byte())        
              End if
            context.Response.BinaryWrite(DirectCast(dReader("Photo"), Byte()))
            End if 
            
            dReader.Close()
            conn.Close()
        End Sub

    Best Regards.

    Brando

    Friday, November 9, 2018 2:10 PM
  • User810354248 posted

    Thanks for your reply.

    The image is not get displaying for which the image is available in database.

    Sunday, December 30, 2018 10:48 AM
  • User810354248 posted

    the code which i had posted in my post is working for me if there is image for that id in DB.

    if the image is not there then i want to show an default image in place of image.

    Sunday, December 30, 2018 10:57 AM