I have a problem when i want to save a file in asp.net...I am using three tier architecture ti store the file
protected void save_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
{
int ID = Convert.ToInt32(Request.QueryString["ID"]);
if (FileUpload1.HasFile)
{
string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (FileExtension.ToLower() != ".doc" && FileExtension.ToLower() != ".docx" && FileExtension.ToLower() != ".pdf")
{
Label1.Text = "Only .doc or .docx or .pdf Files are allowed";
}
else
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
}
}
}
if (IITBAL.Assignments.UpdateFacultyAssignment(ID, SessionId.ToString(), Semester.SelectedItem.Value.ToString(), Course_Name.SelectedItem.Value.ToString(), FileUpload1.FileName.ToString(), date.Text.ToString()))
{
Response.Redirect("Faculty Manage Assignments.aspx");
}
else
{
Label1.Text = "Unable to Add Assignment";
}
}
}
else
{
if (FileUpload1.HasFile)
{
string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (FileExtension.ToLower() != ".doc" && FileExtension.ToLower() != ".docx" && FileExtension.ToLower() != ".pdf")
{
Label1.Text = "Only .doc or .docx or .pdf Files are allowed";
}
else
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
}
}
}
if (IITBAL.Assignments.AddFacultyNewAssignment(SessionId.ToString(),
Semester.SelectedItem.Value.ToString(), Course_Name.SelectedItem.Value.ToString(),UploadFile.FileName.ToString(), date.Text.ToString()))
{
Response.Redirect("Faculty Manage Assignments.aspx");
}
else
{
Label1.Text = "Unable to add Assignment";
}
}
}
}
public static bool AddFacultyNewAssignment(string SessionId, string Semester, string CourseName, string file,string date)
{
string qtext = string.Format("INSERT INTO TblFacultyAssignment(FacultyId,Semester,CourseName,SelectedFile,Date) VALUES ('{0}','{1}','{2}','{3}','{4}')", SessionId, Semester, CourseName, file,date);
return IITDAL.DAL.ExecuteQuerysql(qtext) > 0;
}