InsertRetrieveImagesInGridView.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InsertRetrieveImagesInGridView.aspx.cs" Inherits="InsertRetrieveImagesInGridView" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 152px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Insert and Retrieve Images from Database in Grid View</h1>
<table class="auto-style1">
<tr>
<td class="auto-style2">IMAGE UPLOAD</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="SAVE" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id")%>'></asp:Label>
</td>
<td>
<asp:Image ID="Image1" Height="150" Width="150" ImageUrl='<%# Eval("name")%>' runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
InsertRetrieveImagesInGridView.aspx.cs
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.Configuration;
using System.Data;//It is use for Datatable
using System.IO;
public partial class InsertRetrieveImagesInGridView : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
fillGridView();
}
if(IsPostBack)
{
fillGridView();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string path = Server.MapPath("images/");
if (FileUpload1.HasFile)//HasFile means iska pas file available ho
{
string fileName = Path.GetFileName(FileUpload1.FileName);
string extension = Path.GetExtension(fileName);
HttpPostedFile postedFile = FileUpload1.PostedFile;
int length = postedFile.ContentLength;
if (extension.ToLower() == ".jpg" || extension.ToLower() == ".png" || extension.ToLower() == ".jpeg")
{
if(length <=1000000)
{
FileUpload1.SaveAs(path + fileName);
string name = "images/" + fileName;
string query = "insert into img values(@img)";
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.AddWithValue("@img",name);
con.Open();
//Sirf cmd.ExecuteNonQuery vhi write kar sakta ha
int a = cmd.ExecuteNonQuery();
if(a > 0)
{
Label1.Text = "Inserted Successfull !!";
//It is use for forecolor of label
Label1.ForeColor = System.Drawing.Color.Green;
//It is use to Visible Label
Label1.Visible = true;
fillGridView();
}
else
{
Label1.Text = "Insertion Failed !!";
//It is use for forecolor of label
Label1.ForeColor = System.Drawing.Color.Red;
//It is use to Visible Label
Label1.Visible = true;
}
con.Close();
}
else
{
Label1.Text = "Image file should not be greater than 1 MB !!";
//It is use for forecolor of label
Label1.ForeColor = System.Drawing.Color.Red;
//It is use to Visible Label
Label1.Visible = true;
}
}
else
{
Label1.Text = "Image Format is not Supported !!";
//It is use for forecolor of label
Label1.ForeColor = System.Drawing.Color.Red;
//It is use to Visible Label
Label1.Visible = true;
}
}
else
{
Label1.Text = "Please Upload an Image !!";
//It is use for forecolor of label
Label1.ForeColor = System.Drawing.Color.Red;
//It is use to Visible Label
Label1.Visible = true;
}
}
//Code to View GridView
void fillGridView()
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from img";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable data = new DataTable();
sda.Fill(data);
GridView1.DataSource = data;
GridView1.DataBind();
}
}
0 Comments