.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson
Eminent IT

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Uplaod Image and store in a database using sqlserver 2005

Posted By:Syed Shakeer Hussain       Posted Date: February 16, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Uplaod Image and store in a database using sqlserver 2005

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
public partial class UploadImage : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
Button1 .Text ="click me";

protected void Button1_Click(object sender, EventArgs e)

if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")

byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);

SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connection"].ToString());
SqlCommand storeimage = new SqlCommand("INSERT INTO guest.Photos"
+ "(Picture) "

storeimage.Parameters.Add("@Picture", SqlDbType.Image, myimage.Length).Value = myimage;

storeimage.Parameters["@Picture"].Value = myimage;


Button1.Text = "image";

here picture is my column name.ie belong to image datatype


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend