ImageUploadUsingSqlDataSource

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

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


ImageUpload

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 />

<asp:SqlDataSource

id="srcImages"

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">

<InsertParameters>

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

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

</InsertParameters>

</asp:SqlDataSource>

3.In Default.aspx.cs write following code

protected void btnAdd_Click(object sender, EventArgs e)

{

if (upFile.HasFile)

{

srcImages.Insert();

}

}

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

Vipul
Full Name: vipul vk
Member Level: Starter
Member Status: Member
Member Since: 4/15/2009 9:08:15 PM
Country:

http://www.dotnetfunda.com
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)