Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have a table that it has image feild , and I save image in to my table with binarry format.

but when I whant fetch informatons from my table,special record , it cant show me image.

my code is:
C#
SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=NezamPezeshki;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter("select * from DrInfo " + "where DNum=" + TextBox25.Text, cn);

DataTable dt = new DataTable();
da.Fill(dt);

foreach (DataRow dr in dt.Rows)
{

    TextBox1.Text = (dr["Dname"].ToString());
    TextBox2.Text = (dr["Dfamily"].ToString());
    TextBox3.Text = (dr["Father"].ToString());
    Image1.ImageUrl = "~/Showimg.ashx?id=" +Convert .ToInt32 ( TextBox25.Text);




what do i do? please help me
thanks
Posted
Updated 25-Jul-12 15:40pm
v2
Comments
[no name] 25-Jul-12 21:44pm    
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Get-Image-from-sql-server.html
Wonde Tadesse 25-Jul-12 22:02pm    
Where is the http handler code ?

I am not sure why you're putting all this code in the one place, or is the first bit the code from your ashx handler ? I don't see where you actually read and return this binary data. I think you posted the code to fill your list, but not the code that shows the image. If the ashx is written correctly, this code will work. I don't see why you take the text from textbox25, string mash it in to SQL ( which means your code is not secure, but I take it from the variable name that this is play code and not anything anyone will ever use ), and the convert it to an int, which may blow up, and then back in to a string to be part of a URL ?
 
Share this answer
 
Comments
veusk 25-Jul-12 21:55pm    
I saved picture in another page with handller,
above codes are belong to edit page is for edit information and picture.
I ger key from textbox25, for show information to me,
now ,I dont know ,how get picture information.
Christian Graus 25-Jul-12 21:57pm    
Well, you have posted no code that tries to get it. What is the code for Showimg.ashx ?
veusk 25-Jul-12 22:02pm    
my showing code is:

public class Showimg : IHttpHandler {

public void ProcessRequest (HttpContext context) {
Int32 id;
if (context.Request.QueryString["id"] != null)
id = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");

context.Response.ContentType = "image/jpeg";
Stream strm = ShowEmpImage(id);
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);
}
//context.Response.BinaryWrite(buffer);

}

public Stream ShowEmpImage(int id)
{
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=NezamPezeshki;Integrated Security=True");
// string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;

//SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT pic FROM DrInfo WHERE FileRow= @id";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@id",id);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
veusk 25-Jul-12 22:03pm    
I can save image binary into database, but for fetch it,I have problem
thanks
Christian Graus 25-Jul-12 22:05pm    
What is going wrong ? What happens when you step through the code ? If you rewrite this to build the byte array and write it to the file system, is it an image ? I would just write the whole stream out, not use a loop like this, but this should work, at first glance.
C#
foreach (DataRow dr in dt.Rows)
        {
 
            TextBox1.Text = (dr["Dname"].ToString());
            TextBox2.Text = (dr["Dfamily"].ToString());
            TextBox3.Text = (dr["Father"].ToString());
           Session["ImageBytes"] = //the byte array from db;
       ImagePreview.ImageUrl = "~/ImageHandler.ashx";



then in imagehandler.ashx


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

using System;
using System.Web;

public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest (HttpContext context) {
        //Checking whether the imagebytes variable have anything else not doin anything
        if ((context.Session["ImageBytes"]) != null)
        {
            byte[] image = (byte[])(context.Session["ImageBytes"]);
            context.Response.ContentType = "image/JPEG";
            context.Response.BinaryWrite(image);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}
 
Share this answer
 
Comments
veusk 25-Jul-12 22:54pm    
thanks for your replay
do you have sample code for fetch binary image from database to image control?
thanks
 
Share this answer
 
Comments
[no name] 25-Jul-12 22:41pm    
This is not a solution and a link to the exact code you wanted was given to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900