Pages

Wednesday, February 6, 2008

How to upload an IMAGE in DetailsView ItemInserting event ASP.NET, C#

Now you can upload an images, remarkably easy way. Following code demonstrates that how it can easily implemente in C# code...

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{


if (((FileUpload)DetailsView1.FindControl("FileUpload1")).HasFile)
{


((FileUpload)DetailsView1.FindControl("FileUpload1")).SaveAs(System.Configuration.ConfigurationManager.AppSettings["ItemImagePath"].ToString() + ((FileUpload)DetailsView1.FindControl("FileUpload1")).FileName.ToString());

SqlDataSource1.InsertParameters[8].DefaultValue = ((FileUpload)DetailsView1.FindControl("FileUpload1")).FileName.ToString();

}

}

The code demonstrates that, the image will be saved in the name called "Imagepath" at Web.config file. And the image name will be saved in Database (Datasource1).
Then you should specify the image path in the Web.config file as follows...


<>
< key="ItemImagePath" value="D:\Projects\My Project\www.demoproject.com\images">
< /appSettings >

2 comments:

Jen said...

The blog is helpfull...
visit also asp.net [c#]

virtualSpace said...

very nice blog