---
title: "How to upload images in asp.net using gridview"  
description: "//C# coding//  using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;"  
author: "gautam sen"  
published: 2014-03-05  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/649/how-to-upload-images-in-asp-dot-net-using-gridview  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# How to upload images in asp.net using gridview

\
//C# coding//\
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Data;\
public [partial class](https://www.mindstick.com/forum/155717/define-partial-class-in-mvc) upload_page : System.Web.UI.Page{ [SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable) con = new SqlConnection("[Data Source](https://www.mindstick.com/interview/808/what-is-a-data-source)=GAUTAM-PC\\GKS;Initial Catalog=regis;[Integrated Security](https://www.mindstick.com/forum/159040/what-is-the-difference-between-integrated-security-true-and-integrated-security-sspi)=True"); protected void Page_Load(object sender, EventArgs e) {\
} protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { //[save image](https://www.mindstick.com/forum/439/save-image-in-sql-server) into database string str = FileUpload1.FileName; FileUpload1.PostedFile.SaveAs([Server.MapPath](https://www.mindstick.com/forum/2132/server-mappath-in-asp-dot-net)(".") + "//uploads//" + str); string path = "~//uploads//" + str.ToString(); con.Open(); SqlCommand cmd = new SqlCommand("insert into uploads values('"+TextBox1.Text+"','"+path+"')",con); cmd.ExecuteNonQuery(); con.Close(); Lb1.Text = "Image upload successfully"; //[upload image](https://www.mindstick.com/forum/95341/how-can-use-upload-image-control-in-asp-dot-net) in [grid view](https://www.mindstick.com/articles/893/data-bound-with-grid-view-in-c-sharp-dot-net) [SqlDataAdapter](https://www.mindstick.com/interview/33991/what-is-sqldataadapter) da = new SqlDataAdapter("select * from uploads",con); DataTable dt = new DataTable(); da.Fill(dt); GridView1.DataSource = dt; DataBind(); } else { Lb1.Text = "Plz upload ur image"; } }}\
//default.aspx//\
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload_page.aspx.cs" Inherits="upload_page" %>\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <style type="text/css"> .style1 { width: 100%; height: 42px; } .style2 { width: 210px; } </style></head><body> <form id="form1" runat="server"> <table class="style1"> <tr> <td> <table class="style1"> <tr> <td class="style2"> Name</td> <td> <asp:TextBox ID="TextBox1" runat="server" style="margin-left: 90px" Width="215px"></asp:TextBox> </td> </tr> <tr> <td class="style2"> Photo</td> <td> <asp:FileUpload ID="FileUpload1" runat="server" style="margin-left: 92px" Width="212px" /> </td> </tr> <tr> <td class="style2"> &nbsp;</td> <td> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" style="margin-left: 95px" Text="Upload" Width="214px" /> </td> </tr> <tr> <td class="style2"> <asp:Label ID="Lb1" runat="server" style="font-weight: 700; color: #FF3300" Text="Label"></asp:Label> </td> <td> &nbsp;</td> </tr> <tr> <td class="style2"> <asp:GridView ID="GridView1" runat="server" BackColor="White" AutoGenerateColumns="false" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <RowStyle BackColor="White" ForeColor="#330099" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <SortedAscendingCellStyle BackColor="#FEFCEB" /> <SortedAscendingHeaderStyle BackColor="#AF0101" /> <SortedDescendingCellStyle BackColor="#F6F0C0" /> <SortedDescendingHeaderStyle BackColor="#7E0000" /> <Columns> <asp:TemplateField HeaderText="UID"> <[ItemTemplate](https://www.mindstick.com/forum/2057/access-textbox-in-itemtemplate)> <asp:label ID="lb1" runat="server" Text='<%#Eval("UID") %>'>'></asp:label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:label ID="lb1" runat="server" Text='<%#Eval("Uname") %>'>'></asp:label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Photo"> <ItemTemplate> <asp:Image ID="img" runat="server" ImageUrl='<%#Eval("Image") %>' Height="100" Width="100" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </td> </tr> </table> </td> </tr> </table> </form></body></html>\

---

Original Source: https://www.mindstick.com/blog/649/how-to-upload-images-in-asp-dot-net-using-gridview

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
