Jump to content

display images from sql server in C# with no errors... in C# but with one problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
sweetsugar

sweetsugar

    Newbie

  • Members
  • Pip
  • 2 posts
Hi all (:

How r u doing ?!!

May u help me in my code please?

I have upload different type of images in SQL server.. and i display them in GridView by 2008 visual studio.

every thing is working but, it shows me the image box ( the one with the cross X) and with out any error....why ??!!!!

this is my code:
display.aspx ( has the Gridview with one template field with image control tag):


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="display.aspx.cs" Inherits="display" %>

<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" 
            runat="server">
            <Columns>
               <asp:TemplateField> 
 <ItemTemplate> 
     <asp:Image ID="Image1" runat="server" ImageUrl='<%#    "Handler.ashx?id=" + Eval("picid")  %>' /> 

 </ItemTemplate> 
</asp:TemplateField>
            </Columns>
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

----------------------------------------

the behind code for display.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;


//namespace ImageResizing
//{


public partial class displaypic : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        Byte[] bytImage = imgbin;
        if (bytImage != null)
        {
            Response.ContentType = "image/jpeg";
            Response.Expires = 0; Response.Buffer = true;
            Response.Clear();
            Response.BinaryWrite(bytImage);
            Response.End();

           /* try
             {
               //  System.String _ImgID = System.Convert.ToString(Request.QueryString["picname"]);

                 //System.Int32 _height = System.Convert.ToInt32(Request.QueryString["height"]);

                 //System.Int32 _width = System.Convert.ToInt32(Request.QueryString["width"]);
                [COLOR="red"] String userid;

                 userid = Session["uid"].ToString();[/COLOR]


                 SqlConnection Con = new SqlConnection(" [COLOR="red"]here I have my connection string[/COLOR]");


[COLOR="Red"]// I want the user to be able to display all the images that he upload them in the database by using the session  [/COLOR]


                 System.String SqlCmd = "SELECT * FROM picture where userid = userid";

                 SqlCommand SqlCmdObj = new SqlCommand(SqlCmd, Con);

                 //SqlCmdObj.Parameters.Add("@picname", System.Data.SqlDbType.VarChar, 50).Value = _ImgID;

                 Con.Open();

                 SqlDataReader SqlReader = SqlCmdObj.ExecuteReader();

                 SqlReader.Read();

                 System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";

                 System.Drawing.Image _image = System.Drawing.Image.FromStream(new System.IO.MemoryStream((byte[])SqlReader["piccontent"]));

                 //System.Drawing.Image _newimage = _image.GetThumbnailImage(_width, _height, null, new System.IntPtr());

                // _newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

               
             }
             catch (System.Exception Ex)
             {

                 System.Web.HttpContext.Current.Trace.Write(Ex.Message.ToString());

             }*/
        }

    }
}
//}


-----------------------------------------

the Handler.ashx ( here the image content field will be retrieved from the database and the field datatype is image)

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

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;




public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
       
        SqlConnection myConnection = new SqlConnection();
        myConnection.Open();
        string sql = "Select piccontent from picture"; 
        SqlCommand cmd = new SqlCommand(sql, myConnection);
        cmd.Parameters.Add("@picid", SqlDbType.Int).Value =
        context.Request.QueryString["id"];
        cmd.Prepare();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        context.Response.ContentType = dr["pictype"].ToString();
        context.Response.BinaryWrite((byte[])dr["piccontent"]);
    }
}
----------------------------------------------------------

please help to find why the images in not displaying !! ):


Thaaaaaaaaaaaaaank yooooooooooooooooooooou ^^

Edited by WingedPanther, 22 April 2009 - 03:10 AM.
add code tags (the # button)


#2
Johhny

Johhny

    Newbie

  • Members
  • PipPip
  • 19 posts
holy crap, is this ASP NET? O_O

#3
sweetsugar

sweetsugar

    Newbie

  • Members
  • Pip
  • 2 posts
Page Language="C#" >>>>>>>>>>>> yes it is a C# Johhny and guess what I solve the problem and it is working but I used the GridView (: