Hello
In last article I have shown you how you can
upload a image to database and retrieve and display based on Id on webpage .
here you can View that article Upload image Code
In this article I am going to show how you can
display image in gridview
Step1:
Add a new webpage
Step
2: Put a grid View on newly added webpage
Your aspx page will look like this
<div>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False" DataKeyNames="EmpID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="EmpID" HeaderText="EmpID"
InsertVisible="False"
ReadOnly="True"
SortExpression="ID"
/>
<asp:BoundField DataField="EmpName" HeaderText="EmpName" HeaderStyle-Width="100px" ItemStyle-Height="100px" ItemStyle-Width="100px"
SortExpression="EmpName"
/>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" Width="150px" Height="150px" runat="server"
ImageUrl='<%# "Handler2.ashx?EmpID=" +
Eval("EmpID")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$
ConnectionStrings:DatabaseConnectionString %>"
SelectCommand="SELECT [EmpID],
[EmpName], [EmpPic]
FROM [Tbl_Emp]"></asp:SqlDataSource>
</div>
Step3: Now
add handler (you may refer this link on how to add handler http://simplyaspnet.blogspot.com/2013/08/how-to-save-images-in-database-and.html
) and put below code on
SqlConnection con = new
SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings
["DatabaseConnectionString"].ConnectionString;
// Create SQL Command
SqlCommand cmd = new
SqlCommand();
cmd.CommandText = "Select
EmpName,EmpPic from Tbl_Emp" +
" where EmpID =@EmpID";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new
SqlParameter
("@EmpID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["EmpID"];
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["EmpPic"]);
dReader.Close();
con.Close();
You can
download complete code here Download Code
nice tutorial my bro. I am so happy for becuase it really works. Keep up the good work.
ReplyDeletethanks for liking my post
DeleteSave and Retrieve image from Database and Display in Gridview or list of image in Panel
ReplyDeleteSave and Retrieve Image from Database and Display in Gridview or Panel
How to Display image in gridview in html page before uploading to database ?
ReplyDelete