SIGN UP MEMBER LOGIN:    
ARTICLE

Upload an Image and an Audio file using C#

Posted by Mahak Gupta Articles | ASP.NET Programming October 25, 2010
This article shows how to upload an image and an audio file using C#.
Reader Level:

In my case, I use  a FileUpload Control(Id:->FileUpload1), an ImageButton(ImageButton1) or you can use the Image,  a Button Control(Id:->Updatebttn) to upload my Image, Button Control(Id:->Addbttn) to add my Image in database, an image(which I want to upload), add a new folder (Image) in my WebForm.

Steps:

1. Create a table in the database(abc):->

create table Image1
(
id int identity(1,1),
image1 varchar(100)
);

2. Coding for connectionstring in web.config

<connectionStrings>
    <add name="conn" connectionString="Data Source=MAHAK\SQLEXPRESS;Initial Catalog=abc;Integrated Security=True;"/>
</connectionStrings>

3. In My WebForm(Page1.aspx.cs):

providerName:

using System.Data;
using System.Data.SqlClient;

connectionstring:

SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

4. Uploadbttn Coding:

if (FileUpload1.HasFile)
{
    string fileName = FileUpload1.FileName.ToString();
    string uploadFolderPath = "~/Image/";
    string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
    FileUpload1.SaveAs(filePath + "\\" + fileName);
    ImageButton1.ImageUrl ="~/Image/" + "/" + FileUpload1.FileName.ToString();
}

5. Addbttn Coding:

conn1.Open();
SqlCommand cmd3 = new SqlCommand("insert into Image1(image1) values('" + ImageButton1.ImageUrl + "')", conn1);
cmd3.ExecuteNonQuery();
conn1.Close();

(b) Audio File Upload:

Create a table in the database(abc):

create table Audio1
(
id int identity(1,1),
audio1 varchar(100)
);

Use FileUpload Control and a Button(Id:->Uploadbttn)

In this case add a folder(Audio) in WebForm

Uploadbttn Coding:

if(FileUpload1.HasFile)
{
    string fileName = FileUpload1.FileName.ToString();
    string uploadFolderPath = "~/Audio/";
    string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
    FileUpload1.SaveAs(filePath + "\\" + fileName);
}

Login to add your contents and source code to this article
share this article :
post comment
 

I am very sorry, Please can you help me! I want to insert a picture to SQL database. my project in wpf c#. could you show me how can i do it! Thank you!


Posted by Batya Nov 11, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor