How to upload a image

Syedshakeer
Posted by in ASP.NET category on for Beginner level | Views : 3024 red flag

How to upload a image and store in a database
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());
myConnection.Open();
SqlCommand storeimage = new SqlCommand("INSERT INTO guest.Photos"
+ "(Picture) "

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


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

storeimage.ExecuteNonQuery();

Button1.Text = "image";
}
}
}


here picture is my column name.ie belong to image datatype
Page copy protected against web site content infringement by Copyscape

About the Author

Syedshakeer
Full Name: Syed Shakeer Hussiain P
Member Level:
Member Status: Member
Member Since: 2/5/2009 3:12:18 AM
Country: India
Syed Shakeer Hussain
http://www.dotnetfunda.com
Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)