Create Table in Database:
Column Name DataType Key
id int PK
img varchar(500)
Create Store Procedure:
Insert
create PROC Insert_Image(@img varchar(100))
AS
INSERT INTO Image(img)VALUES(@img)
Retrieve:
create proc sp_Get_Image
as
SELECT img from Image
default.aspx
//Create Images Folder in Project
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width ="80%" cellpadding="0" cellspacing="0" border ="0" align="center">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Image"></asp:Label>
</td>
<td>
<asp:FileUpload ID="UploadFile" runat="server" />
<asp:TextBox ID="txtimg" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
<asp:Button ID="btnShow" runat="server" Text="Show" onclick="btnShow_Click" />
</td>
</tr>
</table>
<br />
<br />
<asp:DataList ID="dlImageGet" runat="server" Height="368px" Width="286px">
<FooterTemplate>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td height="15%" style="background-color: #FF3300">
</td>
</tr>
</table>
</FooterTemplate>
<HeaderTemplate>
<table style="width:100%;">
<tr>
<td style="background-color: #FF0000">
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="50%" cellpadding ="0" cellspacing="0" border ="0">
<tr>
<td class="style1">
<asp:Image ID="img" runat="server" ImageUrl='<%# Bind("~/Images/{0}","img") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
default.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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=DataListDb;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
Upload_Documents();
txtimg.Text = "~/Images/" + UploadFile.FileName;
SqlCommand cmd = new SqlCommand("Insert_Image", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@img", txtimg.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
protected void btnShow_Click(object sender, EventArgs e)
{
try
{
//SqlCommand cmd = new SqlCommand("Get_Records", sqlcon);
SqlDataAdapter da = new SqlDataAdapter("sp_Get_Image", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
dlImageGet.DataSource = ds.Tables[0];
dlImageGet.DataBind();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
protected void Upload_Documents()
{
try
{
// Save the file
string filePath = Server.MapPath("~/Images/" + UploadFile.FileName);
UploadFile.SaveAs(filePath);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
No comments:
Post a Comment