Click here to Skip to main content
14,584,509 members
Rate this:
Please Sign up or sign in to vote.
See more:
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()))
         // IITBAL.Assignment.AddFacultyNewAssignment  mrthod is described below
                {
                    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)
      {
          // query when faculty add new assignment
          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;
      }
Posted
Comments
[no name] 6-Sep-14 8:31am
   
Okay... and? How is this a problem?
mudi lala 6-Sep-14 8:37am
   
Problem is in the two statements...not pick the file and also conversion problem to store the file.
in database the file ha varbinary(max) type ...
if (IITBAL.Assignments.AddFacultyNewAssignment(SessionId.ToString(),
Semester.SelectedItem.Value.ToString(), Course_Name.SelectedItem.Value.ToString(),UploadFile.FileName.ToString(), date.Text.ToString()))



public static bool AddFacultyNewAssignment(string SessionId, string Semester, string CourseName, string file,string date)
{
// query when faculty add new assignment
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;
mudi lala 6-Sep-14 8:38am
   
UploadFile.FileName.ToString(), and string fil(these place have the problem

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

1.The database field used to store the files must be of type Image, if you are using MS SQL.

2.So you should read the file content as bytes arrays (byte[] then send save the bytes into the SQL Image field, like in the next example: http://msdn.microsoft.com/en-us/library/4f5s1we0(v=vs.110).aspx[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100