Home

Saturday, September 28, 2013

INSERT IMAGE INTO MS SQL DATABSE USING ASP.NET

Here i am posting both design view and cod behind.........

1.Design Side Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AccessUpload.aspx.cs" Inherits="AccessUpload" %>

<!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">
body{font-family: tahoma;font-size: 80%;}
.row{clear: both;}
.label{float: left;text-align: right;width: 150px;padding-right: 5px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="row">
<span class="label"><label for="FirstName">First Name: </label></span>
<asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
</div> 
<div class="row">
<span class="label"><label for="Surname">Surname: </label></span>
<asp:TextBox ID="Surname" runat="server"></asp:TextBox>
</div> 
<div class="row"> 
<span class="label"><label for="Photo">Photo: </label></span>
<asp:FileUpload ID="PhotoUpload" runat="server" />
</div>
<div class="row">
<span class="label"><label for="Resume">Resume: </label></span>
<asp:FileUpload ID="ResumeUpload" runat="server" />
</div>
<div class="row">
<span class="label">&nbsp;</span>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
</div> 
</div>
</form>
</body>
</html>

2.Code Side
protected void Button1_Click(object sender, EventArgs e)
{
if (PhotoUpload.HasFile && ResumeUpload.HasFile)
{
Stream photoStream = PhotoUpload.PostedFile.InputStream;
int photoLength = PhotoUpload.PostedFile.ContentLength;
string photoMime = PhotoUpload.PostedFile.ContentType;
string photoName = Path.GetFileName(PhotoUpload.PostedFile.FileName);
byte[] photoData = new byte[photoLength - 1];
photoStream.Read(photoData, 0, photoLength);

Stream resumeStream = ResumeUpload.PostedFile.InputStream;
int resumeLength = ResumeUpload.PostedFile.ContentLength;
string resumeMime = ResumeUpload.PostedFile.ContentType;
string resumeName = Path.GetFileName(ResumeUpload.PostedFile.FileName);
byte[] resumeData = new byte[resumeLength - 1];
resumeStream.Read(resumeData, 0, resumeLength);

string qry = "INSERT INTO Employees (FirstName, LastName, Photo, PhotoFileName, PhotoMime, Resume, 
ResumeFileName, ResumeMime) VALUES (?,?,?,?,?,?,?,?)";
string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|Northwind.mdb";
using (OleDbConnection conn = new OleDbConnection(connect))
{
OleDbCommand cmd = new OleDbCommand(qry, conn);
cmd.Parameters.AddWithValue("", FirstName.Text);
cmd.Parameters.AddWithValue("", Surname.Text);
cmd.Parameters.AddWithValue("", photoData);
cmd.Parameters.AddWithValue("", photoName);
cmd.Parameters.AddWithValue("", photoMime);
cmd.Parameters.AddWithValue("", resumeData);
cmd.Parameters.AddWithValue("", resumeName);
cmd.Parameters.AddWithValue("", resumeMime);
conn.Open();
cmd.ExecuteNonQuery();
}
}