By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

ASP.NET - Help uploading image file and writing it to Access 2007 Db

P: 49
I am developing an application using where a user can enter first name, last name, donation amount, date, et cetera. After the user enters the information and clicks submit, the information is written/stored into an Access 2007 database, which works well.

I am now wanting to allow a user to upload an image of themselves along with their text information and have that image stored in a field of the Access 2007 database.

I have added a file upload control to my interface and I have also added an additional field into my Access database and made it the data type "attachment".

Now here is where I am hitting a roadblock. I know typically in the normal upload/attachment situation you'd add another button to add/upload the file and have a folder to store the uploaded files in. But in this case I want to have the submit button do that.

I'm not sure how to upload the file into the Access 2007 db.

Any help is appreciated! Thanks, Mike
Oct 20 '08 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 663
This example should help

Oct 20 '08 #2

P: 49
This example should help


Thanks for the link, I appreciate it...but unfortunately it did not help.
Oct 20 '08 #3

Expert 100+
P: 663
Thanks for the link, I appreciate it...but unfortunately it did not help.
Did you change the data type of your column to VarBinary?
Oct 20 '08 #4

P: 49
Make the change on the database or the interface? I changed it on the interface and keep getting errors: Cannot create an object of type 'System.TypeCode' from its string representation 'VarBinary' for the 'Type' property.
Oct 20 '08 #5

Curtis Rutland
Expert 2.5K+
P: 3,256
OK, if I remember correctly, you were using the AccessDataSource. I don't think you will be able to do this using that tool.

See, the problem is that you have to save that file to a directory on the server. You can't just go from the fileupload to the DB.

So here's a little sample project I put together to insert and select out a file into a db. Both require the System.Data, System.Data.OleDb, and System.IO namespaces.

The column datatype would be "OLE Object"

Expand|Select|Wrap|Line Numbers
  1. FileUpload1.SaveAs(Server.MapPath("~/upload/" + FileUpload1.FileName));
  2. FileStream fs = new FileStream(Server.MapPath("~/upload/" + FileUpload1.FileName), FileMode.Open, FileAccess.Read);
  3. byte[] buffer = new byte[fs.Length];
  4. fs.Read(buffer, 0, (int)fs.Length);
  5. fs.Close();
  6. string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\dev\test.accdb;Persist Security Info=False;";
  7. string insert = string.Format("insert into test (File) VALUES (@File)", FileUpload1.FileName, buffer);
  8. OleDbConnection conn = new OleDbConnection(connStr);
  9. OleDbCommand cmd = new OleDbCommand(insert, conn);
  10. cmd.Parameters.AddWithValue("@File", buffer);
  11. try
  12. {
  13.     cmd.Connection.Open();
  14.     cmd.ExecuteNonQuery();
  15. }
  17. finally
  18. {
  19.     cmd.Connection.Close();
  20. }
Expand|Select|Wrap|Line Numbers
  1. string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\dev\test.accdb;Persist Security Info=False;";
  2. string select = "SELECT file FROM test WHERE id = 1";
  3. OleDbConnection conn = new OleDbConnection(connStr);
  4. OleDbCommand cmd = new OleDbCommand(select,conn);
  5. object o;
  6. try
  7. {
  8.     cmd.Connection.Open();
  9.     o = cmd.ExecuteScalar();
  10. }
  11. finally
  12. {
  13.     cmd.Connection.Close();
  14. }
  15. byte[] b = o as byte[];
  16. FileStream fs = new FileStream(@"c:\dev\download.xls",FileMode.Create,FileAccess.Write);
  17. fs.Write(b, 0, b.Length);
  18. fs.Flush();
  19. fs.Close();
The second part assumes that you are working with an XLS file, but you can change that to whatever you want.

You could re-write your program to use this mechanism to insert all your fields. Just make sure you add the parameters in the order they show up in the SQL statement.
Oct 23 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.