storing file from a FileUpload control to a database

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 83
Reputation: Elmo_loves_you is an unknown quantity at this point 
Solved Threads: 0
Elmo_loves_you's Avatar
Elmo_loves_you Elmo_loves_you is offline Offline
Junior Poster in Training

storing file from a FileUpload control to a database

 
0
  #1
Apr 8th, 2008
Hi, I would really appreciate some help.

I have a FileUpload control on my Asp.Net web page and want to store the file to a database as a varbinary(max) BLOB with other data via an sproc. However I am getting an error at the line
  1. FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);

ERROR = The given paths format is not supported

I found a sample piece of code that stores a file to a database but it needs a hardcoded File location string (which doesnt quite work with what I want from the fileUpload control). Although I implemented it as a test page and it stores the file (and opens it in another piece of code) just the way I want mine to work.

I know the file from the upload control is already in byte format and that I am creating a new byte array to store the file data but I dont know how to go about doing this other than the way I am currently trying to do it.

protected void Button1_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Today.Date;
            DateTime due_date = DateTime.Parse(txt_dueDate_uploadBrief.Text);
            DateTime return_date = DateTime.Parse(txt_ReturnDate_UploadBrief.Text);
            string modID = txt_HiddenModuleID_UplaodBrief.Text.ToString();
            int module_ID = Convert.ToInt32(modID);
            path = "File:" + FileUpload_uploadBrief.FileName;
            command = new SqlCommand("upload_upBrief", myConnection);
            command.CommandType = CommandType.StoredProcedure;
            myConnection.Open();

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);             bblob = new byte[fs.Length];
            fs.Read(bblob, 0, bblob.Length);
            fs.Close();

            //Initialising new instances of SqlParameter that declares the data type, size and column name
            //for the data that is being passed in
            command.Parameters.Add(new SqlParameter("@assRefNo", SqlDbType.NVarChar, 50, "AssRefNo"));
            command.Parameters.Add(new SqlParameter("@AssignmentBrief", SqlDbType.VarBinary, -1, ParameterDirection.Input, false, 0, 0, "AssignmentBrief", DataRowVersion.Current, path));
            command.Parameters.Add(new SqlParameter("@releaseDate", SqlDbType.Date, 8, "ReleaseDate"));
            command.Parameters.Add(new SqlParameter("@dueDate", SqlDbType.Date, 10, "DueDate"));
            command.Parameters.Add(new SqlParameter("@returnDate", SqlDbType.Date, 8, "ReturnDate"));
            command.Parameters.Add(new SqlParameter("@moduleID", SqlDbType.Int, 4, "ModuleID"));

            ////Adds data based on the parameters passed in
            command.Parameters["@assRefNo"].Value = txt_AssRefNo_uploadBrief.Text.ToString();
            command.Parameters["@AssignmentBrief"].Value = bblob;//FileUpload_uploadBrief.ToString();//test;//value;
            command.Parameters["@releaseDate"].Value = now;
            command.Parameters["@dueDate"].Value = due_date;
            command.Parameters["@returnDate"].Value = return_date;
            command.Parameters["@moduleID"].Value = module_ID;
}

The sample code that uses the hardcoded location is as follows: it also requires that the data already exists in the table

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. myConnection.Open();
  4.  
  5. command = new SqlCommand("UPDATE Pub_info SET logo = @Picture WHERE CategoryName = 'me'", myConnection);
  6. fs = new FileStream("c:\\Builder.doc", FileMode.Open, FileAccess.Read);
  7. Byte[] blob = new Byte[fs.Length];
  8. fs.Read(blob, 0, blob.Length);
  9. fs.Close();
  10. param = new SqlParameter("@Picture", SqlDbType.VarBinary, blob.Length,
  11. ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob);
  12. command.Parameters.Add(param);
  13. command.ExecuteNonQuery();
  14.  
  15. }

Has anyone any ideas? I am rather new to programming.... help help help lol
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: storing file from a FileUpload control to a database

 
0
  #2
Apr 8th, 2008
Please move your thread to ASP.NET forum http://www.daniweb.com/forums/forum18.html
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 83
Reputation: Elmo_loves_you is an unknown quantity at this point 
Solved Threads: 0
Elmo_loves_you's Avatar
Elmo_loves_you Elmo_loves_you is offline Offline
Junior Poster in Training

Re: storing file from a FileUpload control to a database

 
0
  #3
Apr 8th, 2008
I was going to put it there but my problem really isnt ASP its c# .... the FileUpload control can be used in windows forms and asp.net pages so ... plus Im new to this forum and dont know how to move the thread!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: storing file from a FileUpload control to a database

 
0
  #4
Apr 8th, 2008
Never mind friend, I just ask you to get more help, copy it and open another thread in asp.net forum and drop it there and delete this one.

My general answer is to convert the file you get to binary or array of byte (Byte[]) then insert it in the column (of Binary, Image or Blob datatype) and don't forget to insert the extension if the file to help you convert it back when you need to use it again best of luck
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 83
Reputation: Elmo_loves_you is an unknown quantity at this point 
Solved Threads: 0
Elmo_loves_you's Avatar
Elmo_loves_you Elmo_loves_you is offline Offline
Junior Poster in Training

Re: storing file from a FileUpload control to a database

 
0
  #5
Apr 8th, 2008
  1. Byte[] b = FileUpload_uploadBrief.FileBytes;
  2. blob = b;

I got it working, the FileUpload was reading bytes already and I had no need to use FileStream class....all hunky dory now (well just that one wee bit)
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: storing file from a FileUpload control to a database

 
0
  #6
Apr 8th, 2008
Hope your problem got solved, if it did, please mark your thread as solved, else tell me what's up??
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC