Show database image for each usercontrol

Asked

Viewed 625 times

1

Usercontrol1

   private string lastName;
   private string nnovo;                   

   public string LastName
    {
    get { return lastName; }
    set
    {
        lastName = value;
        label2.Text = value;
    }
}
   public string Nnovo {

       get { return nnovo; }
       set {
           nnovo = value;
           label1.Text = value;}
   }

Form

 Button btn = (Button)sender;
        SqlCommand cm = new SqlCommand("SELECT  tblCategory.Categoryname, tblProduct.Productname,tblProduct.Sinopse FROM tblCategory INNER JOIN tblProduct ON tblCategory.Categoryid = tblProduct.Categoryid where tblCategory.Categoryname= '" + btn.Text + "'", cn);

        try
        {
            SqlDataReader dr = cm.ExecuteReader();

            flowLayoutPanel2.Controls.Clear();
            flowLayoutPanel2.Update();
            while (dr.Read())     
            {
                UserControl1 user = new UserControl1();
                user.Nnovo  = (string)dr["Productname"].ToString();//Adiciona os valores do database na label1 do usercontrol
                user.LastName = (string)dr["Sinopse"].ToString(); //Adiciona os valores do database na label1 do usercontrol 


                flowLayoutPanel2.Controls.Add(user);//Carrega usercontrol1 no flowlayoutpanel
                flowLayoutPanel2.Update();

            } dr.Close();           
        }

With the above code I can show each product name and database description on the label of each usercontrol. How to show the database image for each usercontrol’s picturebox?

1 answer

3


Assuming you will use the "Image" tag in your "User Control" (as below), you can set the "Imageurl" as a generic handler. (This is one of the possible solutions)

<asp:Image ID="Image1" runat="server"  ImageUrl="getImage.ashx?id=1" />

And suppose your image in the database is as Varbinary (Filestream). In Handler, you will access the database and return your image in "Response". (see below). I used the Entity Framework for database access.

/// <summary>
/// Summary description for getImage
/// </summary>
public class getImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Clear();
        if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
        {
            int id = Int32.Parse(context.Request.QueryString["id"]);
            Image image = GetImage(id);
            context.Response.ContentType = "image/jpeg";
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        }
        else
        {
            context.Response.ContentType = "text/html";
            context.Response.Write("<p>Need a valid id</p>");
        }
    }

    private Image GetImage(long id)
    {
        var context = new StakeOverFlowEntities();
        Imagens image = (Imagens)context.Imagens.Where(x => x.id == id).FirstOrDefault();
        byte[] contentImage = image.image;
        MemoryStream picture = new MemoryStream(contentImage);
        System.Drawing.Image bitMap = System.Drawing.Image.FromStream(picture);
        return bitMap;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

https://stackoverflow.com/questions/46788/how-to-bind-a-memorystream-to-aspimage-control

Browser other questions tagged

You are not signed in. Login or sign up in order to post.