.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to Show image in a grid view from your local folders

Posted By:Ashutosh Jha       Posted Date: August 31, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article i am going to explain, how we can display any image in a grid view from the computer's local path.
 

In this article i am going to explain, how we can display any image in a grid view from the computer's local path. This article will be useful where you are dealing with some scenarios like, you have to display employee details or you have to bind the item list. In this case you can use the below code which will help you to display together with the other details of the employee/items, you can display their images and that too from your local system.

To start with design, first create a table in your database, suppose empdetails, which will have column as below-

EmpId,
Empname,
Designation and
EmpImg

Then create a gridview, which you can drag and drop from the ASP.NET toolbox controls and their code will be like below-

<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText = "Employee Id" DataField="EmpId" />
<asp:BoundField HeaderText="Employee Name" DataField="Empname" />
<asp:TemplateField HeaderText="Employee Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("EmpImg")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
</asp:GridView>

Now we need to bind this grid view from the database, for this we will write the code to display the data from database in code behind page-

On the page_load method, i am loading the grid again, because all the changes occured in the database will get reflected each time page will refresh.

protected void Page_Load(object sender, EventArgs e)
        {
            Bindgrid();
        }

And now write the method Bindgrid() as below-


Public void Bindgrid()
 {

            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString);
            SqlCommand command = new SqlCommand("SELECT EmpId,Empname,empimg from empdetails", connection);
            SqlDataAdapter daimages = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            daimages.Fill(dt);
            gvImages.DataSource = dt;
            gvImages.DataBind();
        }

That's all. Now run the code and see the output. Your all the details will be shown.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend