How to get multiple images from a database using C #

I have a database of 9 images that are changing, so I can’t directly set the tag srcin html <img>to display 9 images, I have to select them from the database and link them accordingly.

I can get and print 1 image using Response.BinaryWrite(), but not all 9. My byte[]only gets the 1st image from db,

here is the code

            while (dr.Read())
            {
                Response.ContentType = "image/jpg";
                Response.BinaryWrite((byte[])(dr["fsImage"]));

            }

How to get all 9 images and how to attach them to a tag <asp:Image>or build a dynamic html tag <img>dynamically

I am a newbie, so please calm down if there are stupid mistakes;)

Thanks in advance.

+3
source share
2 answers

- .

"" , , . , .

, , URL- , , ,

http://youdomain/makeimage.aspx?imageid=1

http://youdomain/makeimage.aspx?imageid=2

make- .

- , url

http://youdomain/makeimage.aspx?imageid=1&mytype=.jpg

, URL- . (IE) , , . URL-, .jpg.

, , (esp, .)

.

+2

HttpHandler Img

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

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public class Handler2 : IHttpHandler {


    public void ProcessRequest(HttpContext context)
    {

        if (context.Request.QueryString["pid"] != null)
        {
            string ID = context.Request.QueryString["pid"].ToString();

            if (dt.Rows.Count > 0)
            {
                int pid = Convert.ToInt32(ID);
                SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
                myConnection.Open();
                //int i = Convert.ToInt32(context.Request.QueryString["id"]);
                string sql = "Select BackGroundImage from Wall_BackgrndImg_Master where FK_Company_Master=@ImageId";
                SqlCommand cmd = new SqlCommand(sql, myConnection);
                cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = pid;
                cmd.Prepare();
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                try
                {
                    context.Response.ContentType = "jpg";
                    context.Response.BinaryWrite((byte[])dr["BackGroundImage"]);
                    dr.Close();
                    myConnection.Close();
                }
                catch
                {

                }
            }
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

    <img src="~/handlor.ashx?pid=1">
<img src="~/handlor.ashx?pid=2">
<img src="~/handlor.ashx?pid=3">
<img src="~/handlor.ashx?pid=4">
<img src="~/handlor.ashx?pid=5">
+1

All Articles