Hello Check this
Database Tables and Stored Procs
-- Table for storing images
CREATE TABLE [dbo].[ImageGallery](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ImageName] [varchar](50) NULL,
[Images] [varchar](max) NULL
) ON [PRIMARY]
GO
-- Stored Procedure for Inserting Images
create proc [dbo].[InsertImages]
(
@ImageName varchar(50),
@Images varchar(max)
)
as
begin
insert into ImageGallery(ImageName,Images)
values
(@ImageName,@Images)
end
-- Stored Proc for Getting images from database
create proc [dbo].[GetImages]
as
begin
select * from ImageGallery
end
// HTML Mark up and Code behind Code for AddImage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddImage.aspx.cs" Inherits="AddImage" %>
<!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 id="Head1" runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 134px;
}
.style3
{
width: 217px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
Browse Image</td>
<td class="style3">
<asp:FileUpload ID="fupdf" runat="server" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
Image Name</td>
<td class="style3">
<asp:TextBox ID="xtxtImageName" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="btnUpload" runat="server" Text="Upload and Save"
onclick="btnUpload_Click" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
---- Code Behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
public partial class AddImage : System.Web.UI.Page
{
string errdesc = "0";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
// Connection String
string strconn = ConfigurationManager.ConnectionStrings["cnnLocal"].ConnectionString;
SqlConnection con = new SqlConnection(strconn);
// Getting the extension for the images
string extension = Path.GetExtension(fupdf.FileName);
// Getting the File name of Images
string filename = Path.GetFileName(fupdf.FileName);
// This will get the actual path for upload.
string uploadFolder = Request.PhysicalApplicationPath + "Images\\";
// This path will go in database because asp.net requires virtual path.
string upload = "~/Images/" + filename;
// Saving the images in the folder
fupdf.SaveAs(uploadFolder + filename);
string getfile = upload;
SqlCommand cmdins = new SqlCommand("InsertImages", con);
cmdins.CommandType = CommandType.StoredProcedure;
cmdins.Parameters.AddWithValue("@ImageName", SqlDbType.VarChar).Value = xtxtImageName.Text;
cmdins.Parameters.AddWithValue("@Images", getfile);
con.Open();
cmdins.ExecuteNonQuery();
Literal1.Text = "Data Saved";
con.Close();
}
catch (Exception ex)
{
errdesc = ex.Message;
Literal1.Text = errdesc;
}
}
}
Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved
Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator