Uploading And Retrieving Images With SQL Database In ASP.NET Web Forms | ASP.NET
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;
using System.IO;
namespace INSERT_AND_RETRIEVING_IMAGES
{
public partial class WebForm1 : 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)
{
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();
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
Label1.Text = "Inserted Successfully !!";
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Visible = true;
fillGridView();
}
else
{
Label1.Text = "Insertion Failed !!";
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Visible = true;
}
con.Close();
}
else
{
Label1.Text = "Image file should not be greater than 1 MB !!";
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Visible = true;
}
}
else
{
Label1.Text = "Image Format is not supported !!";
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Visible = true;
}
}
else
{
Label1.Text = "Please Upload an Image !!";
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Visible = true;
}
}
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();
}
}
}
Above File is INSERT_AND_RETRIEVING_IMAGES\WebForm1.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="INSERT_AND_RETRIEVING_IMAGES.WebForm1" %>
<!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: 150px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="4" cellspacing="4" 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" Height="35px" OnClick="Button1_Click" Text="SAVE" Width="91px" />
<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" ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</td>
<td>
<asp:Image Height="150" Width="150" ID="Image1" ImageUrl='<%# Eval("Image_Name") %>' runat="server"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment