Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to display an image from folder when there is no image in database

how to display an image from folder when there is no image in database

Scheduled Pinned Locked Moved ASP.NET
csharpdatabaseasp-netdesigntutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    developerit
    wrote on last edited by
    #1

    hi iam using asp.net with c# i want to display an default image from my website folder when there is no image in database , can you correct my code

    <%@ WebHandler Language="C#" Class="left1" %>

    using System;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.UI;

    using System.Web;

    public class left1 : IHttpHandler
    {

    public void ProcessRequest(HttpContext context)
    {
        string  branchid = "";
        
        if(context.Session\["branchid"\]!=null)
        
    
            branchid= context.Session\["branchid"\].ToString();
        else
            throw new ArgumentException("No parameter specified");
    
        context.Response.ContentType = "image/jpeg";
        Stream strm = ShowImage(branchid);
        byte\[\] buffer = new byte\[4096\];
        int byteSeq = strm.Read(buffer, 0, 4096);
    
        while (byteSeq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq = strm.Read(buffer, 0, 4096);
        }
            }
    

    public Stream ShowImage(string branchid)
    {
    SqlConnection con = new SqlConnection(" Data Source=.; Initial Catalog=SafaView;User ID=sa;Password=nsg_ss_0103;");
    string sql = "SELECT imgleft1 FROM tblImages WHERE BranchId = @BranchId";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@BranchId", branchid);
    con.Open();
    object img = cmd.ExecuteScalar();
    try
    {
    return new MemoryStream((byte[])img);
    }
    catch
    {
    return null;
    }
    finally
    {
    con.Close();
    }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    

    }

    in page load =========

    string id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
    id= Session["branchid"].ToString();
    if (IsPostBack == false)
    {
    Image1.ImageUrl = "left1.ashx?BranchId="+id;
    Image2.ImageUrl = "left1old.ashx?BranchId="+id;

           }
    

    i want to display a default image when there is no image in database can you correct my code

    D 1 Reply Last reply
    0
    • D developerit

      hi iam using asp.net with c# i want to display an default image from my website folder when there is no image in database , can you correct my code

      <%@ WebHandler Language="C#" Class="left1" %>

      using System;
      using System.IO;
      using System.Data;
      using System.Data.SqlClient;
      using System.Web.UI;

      using System.Web;

      public class left1 : IHttpHandler
      {

      public void ProcessRequest(HttpContext context)
      {
          string  branchid = "";
          
          if(context.Session\["branchid"\]!=null)
          
      
              branchid= context.Session\["branchid"\].ToString();
          else
              throw new ArgumentException("No parameter specified");
      
          context.Response.ContentType = "image/jpeg";
          Stream strm = ShowImage(branchid);
          byte\[\] buffer = new byte\[4096\];
          int byteSeq = strm.Read(buffer, 0, 4096);
      
          while (byteSeq > 0)
          {
              context.Response.OutputStream.Write(buffer, 0, byteSeq);
              byteSeq = strm.Read(buffer, 0, 4096);
          }
              }
      

      public Stream ShowImage(string branchid)
      {
      SqlConnection con = new SqlConnection(" Data Source=.; Initial Catalog=SafaView;User ID=sa;Password=nsg_ss_0103;");
      string sql = "SELECT imgleft1 FROM tblImages WHERE BranchId = @BranchId";
      SqlCommand cmd = new SqlCommand(sql, con);
      cmd.CommandType = CommandType.Text;
      cmd.Parameters.AddWithValue("@BranchId", branchid);
      con.Open();
      object img = cmd.ExecuteScalar();
      try
      {
      return new MemoryStream((byte[])img);
      }
      catch
      {
      return null;
      }
      finally
      {
      con.Close();
      }
      }

      public bool IsReusable
      {
          get
          {
              return false;
          }
      }
      

      }

      in page load =========

      string id = "";
      protected void Page_Load(object sender, EventArgs e)
      {
      id= Session["branchid"].ToString();
      if (IsPostBack == false)
      {
      Image1.ImageUrl = "left1.ashx?BranchId="+id;
      Image2.ImageUrl = "left1old.ashx?BranchId="+id;

             }
      

      i want to display a default image when there is no image in database can you correct my code

      D Offline
      D Offline
      Dinesh Mani
      wrote on last edited by
      #2

      My assumptions - * Default image is also in the database * BranchId is numeric * Default image has the least possible BranchId say 0 Alter the query like so -

      SELECT top 1 imgleft1 FROM tblImages WHERE BranchId in (@BranchId, @Default) order by BranchId desc

      Add this line to the code

      cmd.Parameters.AddWithValue("@Default", 0);

      Now if the BranchId is available then that image would be displayed, else the default image would be displayed. HTH!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups