Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I stored image from a folder to mysql database.image column type mediumblob..
but I cant come back to gridview image control inside templatefield

I am seeing icon but image not comess...

I think binary store and binary convert to image again problem..

WHAT İS THE WRONG?
image insert code

string fileName = fileuploadImage.PostedFile.FileName;
               int fileLength = fileuploadImage.PostedFile.ContentLength;

               byte[] imageBytes = new byte[fileLength];
               fileuploadImage.PostedFile.InputStream.Read(imageBytes, 0, fileLength);

               MySqlConnection conn1 = new MySqlConnection(baglan.connStr1);
               conn1.Open();

             string   sql = "INSERT INTO kayit(ogrno,resim) VALUES" +
                   "('" + fileName + "'" +

                   ",'" + imageBytes + "')";
               MySqlCommand cmd = new MySqlCommand(sql, conn1);
               int count=cmd.ExecuteNonQuery();
               conn1.Close();


image comeback code

this is gridview source

XML
<asp:TemplateField HeaderText="resim">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "xxx.ashx?ImID="+ Eval("id") %>' Height="150px" Width="150px"/>
</ItemTemplate>
</asp:TemplateField>


string imageid = context.Request.QueryString["ImID"];
           MySqlConnection connection = new MySqlConnection(baglan.connStr1);
           connection.Open();
           MySqlCommand command = new MySqlCommand("select resim from kayit where id=" + imageid, connection);
           byte[] buf = (byte[])command.ExecuteScalar();


           context.Response.Clear();
           context.Response.OutputStream.Write(buf, 0, buf.Length);
           context.Response.ContentType = "image/jpeg";
           context.Response.BinaryWrite(buf);
           connection.Close();
           context.Response.End();
this is code
Posted
Updated 4-Jun-13 21:06pm
v3
Comments
Can you simple debug the ashx handler by putting a debugger in that file and check what its fetching.

And check in DataBase table, if data is getting saved or not in the column "resim" .
Member-2338430 5-Jun-13 4:24am    
resim column has 13 byte data so image saved
Member-2338430 5-Jun-13 4:25am    
after debug everything ok there is no error anywhere but not seeing image only icon
Check what are you getting in buf variable?
Bojjaiah 5-Jun-13 8:07am    
what is the database structure you have? for saving image

1 solution

try this in handler.ashx page


C#
public void ProcessRequest(HttpContext context)
    {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        string imageId = context.Request.QueryString["ImID"];
        SqlConnection con = new SqlConnection(strcon);
        con.Open();
        SqlCommand command = new SqlCommand("select resim from kayit where id=" + imageId, con);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[0]);
        con.Close();
        context.Response.End();


    }


How to Insert and Retrieve Image from DataBase[^]
 
Share this answer
 
v2
Comments
Member-2338430 5-Jun-13 3:30am    
I tried before not displaying image
Member-2338430 5-Jun-13 3:32am    
I tried to everthing this code idea true?
Bojjaiah 5-Jun-13 5:00am    
see updated my code
Member-2338430 5-Jun-13 6:19am    
why not run?
Bojjaiah 5-Jun-13 8:45am    
what is the database structure you have? for saving image

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