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

These code can insert image in database using sqldatasource.
These code can insert image in database using sqldatasource


Follow following steps

1.Take one web Application

2.Add one Webform

2.In Default.aspx write following code

<asp:Label ID="lblFile"

Text="Image File"

runat="Server" />

<asp:FileUpload ID="upFile"

runat="Server" />

<asp:Button ID="btnAdd"

Text="Add Image"

runat="Server" OnClick="btnAdd_Click" />

<hr />



ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ImageUpload\App_Data\ImagesDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

SelectCommand="select FileName from Images"

InsertCommand="insert images (FileName,Image)values(@FileName,@FileBytes)"

runat="Server" ProviderName="System.Data.SqlClient">


<asp:ControlParameter Name="FileName" ControlID="upFile" PropertyName="FileName" />

<asp:ControlParameter Name="FileBytes" ControlID="upFile" PropertyName="FileBytes" />



3.In Default.aspx.cs write following code

protected void btnAdd_Click(object sender, EventArgs e)


if (upFile.HasFile)





4.Create Table

Give Name Images.

1.Id int(Primary Key)

2.FileName nvarchar(150) not null

3.Image varbinary(Max) =null


Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: vipul vk
Member Level: Starter
Member Status: Member
Member Since: 4/15/2009 9:08:15 PM
Working as a software developer

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)