blog

Home / DeveloperSection / Blogs / How to upload images in asp.net using gridview

How to upload images in asp.net using gridview

gautam sen4203 05-Mar-2014

//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 upload_page : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=GAUTAM-PC\\GKS;Initial Catalog=regis;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            //save image into database
            string str = FileUpload1.FileName;
            FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//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 in grid view
            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>
                                <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>


Updated 18-Sep-2014
hi, I'm a beginner, want to learn more and I'm on a dot-net platform.

Leave Comment

Comments

Liked By