You need to add the DropDownList Selected Index change event. If that event set the Selected value Session.
HTML
<asp:ToolkitScriptManager ID="ToolkitScriptManager" runat="server">
</asp:ToolkitScriptManager>
<div align="center">
<asp:DropDownList ID="ddlFileType" runat="server" OnSelectedIndexChanged="FileType_OnSelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" AllowedFileTypes="jpg,jpeg,png,gif"
MaximumNumberOfFiles="3" OnUploadComplete="File_Upload" Width="500px" />
Namespaces
using AjaxControlToolkit;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
PopulateDropDownList();
}
}
protected void FileType_OnSelectedIndexChanged(object sender, EventArgs e)
{
Session["SelectedValue"] = this.ddlFileType.SelectedItem.Value;
}
private void PopulateDropDownList()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection cn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM FileType", cn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.ddlFileType.DataTextField = "Type";
this.ddlFileType.DataValueField = "Id";
this.ddlFileType.DataSource = ds;
this.ddlFileType.DataBind();
}
}
}
}
protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
string filename = Path.GetFileName(e.FileName);
string strDestPath = Server.MapPath("~/Documents/");
string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
string strQuery = "INSERT INTO FilesUploaded (FileType, FileName, FilePath) VALUES(@FileType,@FileName, @FilePath)";
using (SqlCommand cmd = new SqlCommand(strQuery))
{
cmd.Parameters.AddWithValue("@FileType", Convert.ToInt32(Session["SelectedValue"]));
cmd.Parameters.AddWithValue("@FileName", filename);
cmd.Parameters.AddWithValue("@FilePath", "Documents/" + filename);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
//Save file to project
AjaxFileUpload1.SaveAs(@strDestPath + filename);
}
SQL
CREATE TABLE [dbo].[FileType](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Type] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT INTO [dbo].[FileType]
([Type])
VALUES
('Flower')
GO
INSERT INTO [dbo].[FileType]
([Type])
VALUES
('Animal')
GO
INSERT INTO [dbo].[FileType]
([Type])
VALUES
('Scenery')
GO
CREATE TABLE [dbo].[FilesUploaded](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FileType] [varchar](50) NULL,
[FileName] [varchar](100) NULL,
[FilePath] [varchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
Screenshot
FileType is DropDownList value.
