Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code of saving image database
C#
protected void BtnSave_Click(object sender, EventArgs e)
        {
         
            SqlConnection con = new SqlConnection(connectionString);
            if (FileUpload1.HasFile)
            {
                if (FileUpload1.PostedFile.ContentType == "image/bmp" || FileUpload1.PostedFile.ContentType == "image/jpg" || FileUpload1.PostedFile.ContentType == "image/jpeg" || FileUpload1.PostedFile.ContentType == "image/png")
                {
                    int filelenght = FileUpload1.PostedFile.ContentLength;
                    byte[] imagebytes = new byte[filelenght];
                    FileUpload1.PostedFile.InputStream.Read(imagebytes, 0, filelenght);
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "Insert into imagesave(image) values(@img)";
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@img", imagebytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Response.Write("Image saved to database");
                }
            }
        }


ASP.NET
  <asp:DataList ID="DataList1" runat="server" BorderStyle="None" Width="634px" 
                                            BorderWidth="1px" RepeatColumns="3" BackColor="White" DataKeyField="ProductSNo"
                                            BorderColor="#CCCCCC" CellPadding="4" ForeColor="Black" 
                         >
                                          
                                            <footerstyle backcolor="#CCCC99" forecolor="Black" />
                                            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                                            <itemtemplate>
                                                <table cellspacing="1" style="border-style: solid">
                                                    <tr>
                                                        <td align="center">
                                                <asp:Label ID="lblProductName" runat="server"  Text='<% #DataBinder.Eval(Container.DataItem, "ProductName")%>' Font-Bold="True" Font-Underline="True" Font-Size="X-Large"><br /><hr />

                                                        </td>
                                                   </tr>
             
                                                    <tr>
                                                        <td>


Now how  to reteieve image in datalist:

                                                        <asp:Image ID="imgProduct"   ?????????</td></tr></table></itemtemplate>
Posted
Updated 5-Sep-15 22:09pm
v2

1 solution

You are just few lines of conventional code away from the solution. If you have written the code to INSERT by your own then it should not be a great task to do.

Get data from database in to a Datatable object and set it as datasource of the DataList.

C#
using (SqlDataAdapter da = new SqlDataAdapter("SELECT [image],ProductName FROM imagesave", con)) //You may need to use JOIN to get ProductName if it resides in another table
{
    DataTable dt = new DataTable();
    da.Fill(dt);
    DataList1.DataSource = dt;
}


You can use simple html img instead of ASP.Net Image.
HTML
<img src='<% #DataBinder.Eval(Container.DataItem, "image")%>' />


Note: it is not recommended to use keywords for naming column, you can consider changing your column name to something like ProductImage.

Hope, it helps :)
 
Share this answer
 
v2
Comments
Member 11804811 6-Sep-15 8:31am    
Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1024: Preprocessor directive expected

Source Error:


Line 113: <tr>
Line 114: <td>
Line 115: <img src="<% #DataBinder.Eval(Container.DataItem, "image")%>" />
Line 116:
Line 117: </td>
Member 11804811 6-Sep-15 8:32am    
Thanku...But When I am taking <img> tag,it gives This Error.Help me to sort Out this
Suvendu Shekhar Giri 6-Sep-15 9:02am    
Wrap content within single quotes rather than double quotes for src attribute. Check the updated solution.
<img src='<% #DataBinder.Eval(Container.DataItem, "image")%>' />
Member 11804811 6-Sep-15 9:07am    
Got The Same Error Again
Member 11804811 6-Sep-15 9:08am    
An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

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