Uploading multiple files using FileUpload
This resources about Asp.net upload control.In this resources we have given how to upload images and files and how to save into database with the help of using three tier architecture and how to make connection in web.config file
Read this article to learn how to use the File upload control in ASP.NET.Requirements
Visual Studio, Visual Web Developer
Basic Knowledge of ASP.NET
Basic concept of developing web applicationsAfter Reading this
1. You will learn, how to upload images and files.
2. How to saves that images and files into database.
3. How to use three tier architecture.
4. How to change theme at Runtime.
5. How to make connection through Configuration file (web.config).
Preparing SolutionCreate a Empty Web Site in ASP.NET.
1. Add a ASP.NET default page
2. Add three class file in App_Code (App_Code will automatically generate)
3. Add three class file name
a. DBConnect.cs
b. Modulemaster_BLL.cs
c. ModuleMaster)DAL.csNow come to Default.aspx code
< table width="50%" border="0" cellspacing="0" cellpadding="0">
< tr>
< td>
< asp:Label ID="lblError" runat="server" Text="Label">< /asp:Label>
< /td>
< /tr>
< tr>
< td>
Upload Image:
< /td>
< td>
< asp:FileUpload ID="UpImage" runat="server" TabIndex="20" />
< /td>
< /tr>
< tr>
< td colspan="4" align="left" valign="top">
< /td>
< /tr>
< tr>
< td colspan="4" align="center" valign="top">
< asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Submit_Click" />
< /td>
< /tr>
< /table>
Above code will produce below Design.Backend code of Default.aspx (C#)
This code is for Button Click event event and function. .
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
string imgPath = "";
ModuleMaster_BLL oModuleMaster_BLL = new ModuleMaster_BLL();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
string ModuleImage = UploadImage();
if (ModuleImage == "invalid")
{
lblError.Text = "Please select Image extension between .jpg, .gif, .png, .jpeg , .bmp";
return;
}
else if (ModuleImage == "size")
{
lblError.Text = "file size should not exceeds 2MB";
return;
}
else
{
oModuleMaster_BLL.ModuleImage = ModuleImage;
int Res = oModuleMaster_BLL.Insert_Module();
}
}
public string UploadImage()
{
string filename = "";
//upload Image
string strFileNameWithPath = UpImage.PostedFile.FileName;
string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPath);
string strFileName = System.IO.Path.GetFileName(strFileNameWithPath);
if (strFileName.Equals(""))
{
imgPath = "select_image.jpg";
return imgPath;
}
else
{
string filePath = "ModuleImages/";
string file1Extension = System.IO.Path.GetExtension(UpImage.FileName);
int file1Size = UpImage.PostedFile.ContentLength;
if (UpImage.HasFile)
{
if (file1Extension.Equals(".jpg") || file1Extension.Equals(".png") || file1Extension.Equals(".jpeg") || file1Extension.Equals(".gif")
|| file1Extension.Equals(".JPG") || file1Extension.Equals(".PNG") || file1Extension.Equals(".JPEG") || file1Extension.Equals(".GIF"))
{
if (file1Size < 2000000)
{
filename = ID + "-" + UpImage.FileName.ToString();
UpImage.SaveAs(Server.MapPath(@filePath + filename));
return filename;
}
else
{
filename = "size";
return filename;
}
}
else
{
filename = "invalid";
return filename;
}
}
}
return filename;
}
}Module master BLL Class File (C#)
This code is for ModuleMaster_BLL. .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
public class ModuleMaster_BLL
{
ModuleMaster_DAL oModuleMaster_DAL = new ModuleMaster_DAL();
public ModuleMaster_BLL()
{
//
// TODO: Add constructor logic here
//
}
#region property declaration with set and get accessor
string _ModuleImage;
public string ModuleImage
{
get { return _ModuleImage; }
set { _ModuleImage = value; }
}
#endregion
#region Admin Methods
#region Insert Module
public int Insert_Module()
{
return oModuleMaster_DAL.Insert_Module(this);
}
#endregion
#endregion
}Module master DAL Class File (C#)
This code is for ModuleMaster_DAL. .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class ModuleMaster_DAL : DBConnect
{
public ModuleMaster_DAL()
{
//
// TODO: Add constructor logic here
//
}
#region Admin Methods
#region Insert Module
public int Insert_Module(ModuleMaster_BLL oModuleMaster_BLL)
{
SqlConnection oCon = new SqlConnection(strDBCon);
try
{
oCon.Open();
SqlCommand oCom = new SqlCommand("sp_ModuleMaster", oCon);
oCom.CommandType = CommandType.StoredProcedure;
oCom.Parameters.Clear();
oCom.Parameters.Add("@ModuleImage", SqlDbType.VarChar, 200).Value = oModuleMaster_BLL.ModuleImage;
int ID = oCom.ExecuteNonQuery();
SqlCommand cmd = new SqlCommand("", oCon);
cmd.CommandText = "SELECT @@Identity";
int ModuleID = Convert.ToInt32(cmd.ExecuteScalar());
return ModuleID;
}
catch (Exception ex)
{
return 0;
}
finally
{
oCon.Close();
oCon.Dispose();
}
}
#endregion
#endregion
}DB Connect Class File (C#)
This code is for DBConnect Class file. .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
public class DBConnect
{
protected readonly string strDBCon;
public DBConnect()
{
strDBCon = ConfigurationManager.ConnectionStrings["Sampledbname"].ToString();
}
}Configuration file (Web.Config) code (C#)
This code is for Configuration file. .
<connectionStrings>
<add name="Sampledbname" connectionString="Data Source=./;Initial Catalog=Sampledbname;Integrated Security=True" providerName="System.Data.SqlClient"/>
< /connectionStrings>