The following steps is shows about insert a image into database by using
asp.net.
First Create a Image table in sqlserver database with two fields ImageName and Image.
Then
1.Open VisualStudio 2010
2.FileàNewàWebsiteàselect Empty Website àGive name as Imageà Select Location to saveà Save
3.Select Solution àright click à Add New ItemàWebFormàGive name as ImageLoad.aspx
4.Write the following Code in ImageLoad..aspx
First Create a Image table in sqlserver database with two fields ImageName and Image.
Then
1.Open VisualStudio 2010
2.FileàNewàWebsiteàselect Empty Website àGive name as Imageà Select Location to saveà Save
3.Select Solution àright click à Add New ItemàWebFormàGive name as ImageLoad.aspx
4.Write the following Code in ImageLoad..aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageLoad.aspx.cs" Inherits="ImageLoad"
%>
<!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">
.style1
{
width:
100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table
class="style1">
<tr>
<td>
<asp:Label ID="lbImageName" runat="server" Text="ImageName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtImageName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbImage" runat="server" Text="Image"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbmsg" runat="server" Text="Label" ></asp:Label>
</td>
<td>
<asp:Button ID="btnUpload" runat="server" onclick="btnUpload_Click1"
Text="Upload" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
5.Write the following Code in
ImageLoad.aspx.cs file
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.Data;
public partial
class ImageLoad
: System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void btnUpload_Click1(object
sender, EventArgs e)
{
string strImageName = txtImageName.Text.ToString();
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
byte[]
imageSize = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile
uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
// Create
SQL Connection
SqlConnection
con = new SqlConnection("data Source=ADMIN;initial catalog=test; user id=Sa;
password=123");
// Create
SQL Command
SqlCommand
cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Image(ImageName,Image)" + " VALUES (@ImageName,@Image)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlParameter
ImageName = new SqlParameter("@ImageName", SqlDbType.VarChar,
50);
ImageName.Value =
strImageName.ToString();
cmd.Parameters.Add(ImageName);
SqlParameter
UploadedImage = new SqlParameter("@Image", SqlDbType.Image,
imageSize.Length);
UploadedImage.Value = imageSize;
cmd.Parameters.Add(UploadedImage);
con.Open();
int
result = cmd.ExecuteNonQuery();
con.Close();
if
(result > 0)
lbmsg.Text = "File Uploaded";
}
}
}
6.See the Output
7.Then the Image will store in database with Binary Data.
Thanks for the post!!!
ReplyDeleteits nod inserting in database...
ReplyDeletewhere you are getting error ?
Deleteyou may find this useful:
ReplyDeletehttp://webgeek.elletis.com/how-to-enter-html-code-into-database-from-a-textbox/
thnaks ..
ReplyDeleteyour code was helpful for me..
i have to insert an image into mysql using jquery in MVC3.
ReplyDeletecan anyone help me???????
please.
it's not uploading image to database
ReplyDeleteSir its two time inserting my image can u help me please
ReplyDeletesir i want to give a particular size for images
ReplyDeletewhere i have to give
ReplyDeleteHey, nice site you have here! Keep up the excellent work!
Elletis Web Geek
Thank you very much...
ReplyDeleteAnd i want coding for retrieve image from database,please help me...
I am getting an error like this..
ReplyDeleteA network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Source Error:
Line 54: cmd.Parameters.Add(UploadedImage);
Line 55:
Line 56: con.Open();
Line 57:
Line 58: int result = cmd.ExecuteNonQuery();
Source File: c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WebApplication2\WebApplication2\WebForm1.aspx.cs Line: 56
Finally i changed user id and password and its not establish the connection pls help me to solve this problem immediately
ReplyDeleteI want To uplaod image from any folder by providing virtual path means any thing else but without use of file upload
ReplyDelete.Net Tutorial: Insert Image Into Sqlserver Database By Using C Asp.Net : >>>>> Download Now
ReplyDelete>>>>> Download Full
.Net Tutorial: Insert Image Into Sqlserver Database By Using C Asp.Net : >>>>> Download LINK
>>>>> Download Now
.Net Tutorial: Insert Image Into Sqlserver Database By Using C Asp.Net : >>>>> Download Full
>>>>> Download LINK