Its really different problem.please someone help me. in retrieving image stored in sql databaseto to a page or to image control. i tried everything since last four days.. pls help.
HI iam yogesh, i am not new to asp.net or c#. i have succesfully stored images in sql table(datatype-> image), using the following code
HttpPostedFile file = myfile.PostedFile;
int size = file.ContentLength;
string strtype = file.ContentType;
string fn = file.FileName;
byte[] bit = new byte[size];
file.InputStream.Read(bit, 0, bit.Length);
SqlCommand savecmd = new SqlCommand("insert into reginfo (userid,password,name,surname,sex,country,imgdata,imgsize,imgstrtype,imgfilename) values (" + "'" + txtregid.Text + "'," + "'" + txtregpassword.Text + "'," + "'" + txtname.Text + "'," + "'" + txtsurname.Text + "'," + "'" + ddsex.Text + "'," + "'" + ddcountry.Text + "'"+","+"'"+bit+"'"+","+size+","+"'"+strtype+"'"+","+"'"+fn+"'"+")", methods.connection);
savecmd.ExecuteNonQuery();
I USED THE FOLLOWING IN MYPAGE.ASPX----
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
now when iam trying to fetch the image from table on html image control it shows the image control without image. it also shows a small image icon on upper left corner.
i have also used image.ashx (handler) but still failed. my code with handler and without handler are:
WITH HANDLER--------
using System;
using System.Data;
using System.Web;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using salefunctions;
namespace ADVANCED_FORUM
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class iamge : IHttpHandler
{
public string quid;
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["user"]!=null)
{
quid = context.Request.QueryString["user"];
context.Response.ContentType="image/jpeg";
Stream strm = showimage(quid);
byte[] buffer = new byte[4096];
int buffseq = strm.Read(buffer,0, 4096);
while(buffseq>0)
{
context.Response.OutputStream.Write(buffer, 0, buffseq);
buffseq = strm.Read(buffer,0, 4096);
}
}
}
public Stream showimage(string id)
{
methods.createcon("forum");
SqlCommand dispimgcmd = new SqlCommand("select imgdata from reginfo where userid=@aid", methods.connection);
dispimgcmd.Parameters.AddWithValue("@aid", id);
object mimg = dispimgcmd.ExecuteScalar();
return new MemoryStream((byte[])mimg);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
CODE WITHOUT HANDLER (TO DISPLAY IMAGE ON PAGE);
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using salefunctions;
namespace ADVANCED_FORUM
{
public partial class imageview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
methods.createcon("forum");
SqlCommand dispimgcmd=new SqlCommand("select * from reginfo where userid=" + "'" + Session["mlinkquid"] + "'", methods.connection);
SqlDataReader drdispimg = dispimgcmd.ExecuteReader();
drdispimg.Read();
Response.Buffer = true;
Response.Clear();
Response.ContentType = (string)drdispimg["imgstrtype"];
Response.OutputStream.Write((byte[])drdispimg["imgdata"],0,(int)drdispimg["imgsize"]);
methods.connection.Close();
Response.End();
}
}
}
plsssssssssssss someone help me... i have checked everything,contenttype,contentlength.all is ok but iam not getting the image.
i think there is some problem with iamges. because even when i dragged an img control and set "src" property as
src="d:\images\srk.jpg"
or
src="~/images/srk.jpg"
it shows the image atdesign time but it dosent shows the same (at runtime) i:e when i run the project, the same problem happens, img control displays with no image.
plsssssssssssssss iam tired of asking for solution. pls help me. its very imp for me... IF THERE ARE ANY MASTERS IN ASP,C#
[edit]SHOUTING removed, Subject - OriginalGriff[/edit]