Last post Jan 02, 2009 05:58 PM by rami_nassar
Dec 30, 2008 03:07 PM|SmartOfStupid|LINK
Hello there, i'm struggling with getting a file saved into the database. I discovered the FileUpload control, and i can successfully save the filename into the database. However i'm stuck on actually getting the binary data into the data. bo.ImageData is
a Byte and links to a varbinary field in the database.
If ImageInfo.HasFile Then
Dim bo As tblImages
bo = New tblImages(PDSAAppConfig.ConnectString)
.InsertFilter = tblImagesDC.InsertFilters.All
.FileName = ImageInfo.FileName
.ImageData = <WHAT GOES HERE>
.DateAdded = Today.ToString()
Thank you so much in advance. Probably something very simple but i'm getting very flustered.
P.S i am using Visual Studio 2008 and vb.net 3.5
Upload image database
store binary file database
Dec 30, 2008 03:22 PM|Kumar Reddi|LINK
You need to convert the content of your file into byte stream and store the binary data in the database..I am going to give you some c# code that I used in one of my application, just so you get the idea.
byte fileBuffer = new byte[fileAttachBox.PostedFile.ContentLength];
Stream objStream = fileAttachBox.PostedFile.InputStream;
Dec 30, 2008 07:03 PM|hans_v|LINK
Storing Binary Files Directly in the Database Using ASP.NET 2.0
Dec 30, 2008 07:32 PM|SmartOfStupid|LINK
Kumar: I'm looking into your suggestion right now. I didn't see any 'stream' datatype offhand but i'll keep looking.Thanks so much for your help.
Hans: I tried your suggestion but i'm getting an error.
Dim length As Integer = Convert.ToInt32(ImageInfo.PostedFile.InputStream.Length)
Dim imageBytes(length) As Byte
ImageInfo.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
.ImageData = imageBytes
.ImageData = imageBytes LINE ERROR: '1-dimensional array of Byte' cannot be converted to 'Byte'
I realize its some sort of data conversation issue but i'm still figuring out what to do about it. Sorry for the newbie issues.
Dec 30, 2008 07:48 PM|SmartOfStupid|LINK
Kumar: ah-ha i discovered IO.Stream.
Dim imageBytes(Convert.ToInt32(ImageInfo.PostedFile.InputStream.Length)) As Byte
Dim stream As IO.Stream = ImageInfo.PostedFile.InputStream
stream.Read(imageBytes, 0, imageBytes.Length)
However, i get the same 1 dimensional array problem as mentioned above. If i try to convert it bo.ImageData = Convert.ToByte(imageBytes) i get an exception when i run the code.
"Unable to cast object of type 'System.Byte' to type 'System.IConvertible'."
Dec 30, 2008 09:04 PM|Kumar Reddi|LINK
You must have guessed it by now, bo.ImageData must be of type byte array. not a byte type.
Please double check the type of the imageData..
Jan 02, 2009 05:58 PM|rami_nassar|LINK
check this artichles