I am using following code to get data from database and load to picture box
Dim vrPicFromDB = IO.File.ReadAllBytes(DsPic.tblPicTest.Item("Picture"))
Dim ms As New MemoryStream(vrPicFromDB)
PictureBox1.Image = Image.FromStream(ms)
It gives error on DsPic.tblPicTest.Item("Picture")) portion of the statement.
I also tried
CByte(DsPic.tblPicTest.Item("Picture")))
but it gives the same error.
Please advise how to fix it.
Thanks
Furqan
Assuming that the "Picture" column is an Image column in the database, your line to load the vrPicFromDB byte array will look something like this:
Dim vrPicFromDB As Byte()
vrPicFromDB = CType(DsPic.tblPicTest.Rows(0).Item("Picture"), Byte())
That line assume you have loaded at least 1 row of data.
Related
so I am writing a program and trying to setup the save/open features of the program. I have the Save feature working just fine, but can't get the open feature to work.
The issue I'm running into is pulling the data from the text file to the form to fill in the multiple fields and controls. my example code is below
Imports System.IO
Main 1
Sub openFile_Click(sender, e) handles openFile.Click
Dim lineIndex As Integer = 12 'this is my total lines in my file
ofdRead.ShowDialog()
If ofdRead.FileName <> "" then
sr = New StreamReader(ofdRead.FileName)
For i As Integer = 0 To lineIndex -1
sr.ReadLine()
Next
txtField1.Text = sr.ReadLine
cboBox1.SelectedIndex = sr.ReadLine
'this continues through all fields til complete
sr.Close()
End If
End Sub
End Class
I keep getting an error for anything that is being returned as not being a string, and it seems as though the data is being read in reverse as well according to my error output.
Any help would be much appreciated (been searching and pouring over forums for 2 days looking for help)
Thanks Tim Schmelter for your insight. I was calling the wrong data type for the cboBox1 variable. Here is my corrected code (without the For-Loop, turns out i didn't need it)
cboBox1.SelectedItem = sr.ReadLine
so everytime I run into something like that I just have to tell it to put it in as a string instead of an integer
I have a text file with records of machines stored on it, a single record looks like this:
"1234567890","12/04/2013","Saw","Hilti","17/11/2012",#TRUE#,#FALSE#,#FALSE#,"Made odd noise when operating"
What I would like to achieve is to populate a combo box with only the first item of each record - the serial number - so as to be able to search for the machine in a form based on the selected serial number. I need it to read the file, pick out the serial number from each record, fill the combo box with them, then allow you to select the one that you want.
Thanks in advance for any help.
if you are using a CSV formatted text file you can store info in a datatable and store each datarow in your cb, and use the DisplayMember property of the combobox to show only the first column. You also need that first line has the names of the columns to achieve this.
Try this supposing the first column is called Serial:
For Each r as DataRow in table.Rows
combobox1.Items.Add(r)
Next
combobox1.DisplayMember("Serial")
If not, you can still use your streamreader to obtain each line and split it to obtain first field.
Try:
Do
Dim line As String = reader.readLine()
If line Is Nothing Then Exit Do
Dim fields as String() = Split(line, ",")
combobox1.Items.Add(fields(0))
Loop
Hope it helps. You may have to use SubString method to retire the quotes. I post it apart:
replace
combobox1.Items.Add(fields(0))
by
combobox1.Items.Add(fields(0).SubString(1,fields(0).length -1))
If it's a simple app and you're happy to use the reader mentioned above, you can also do the following. I've used Replace() instead of Substring().
Dim reader As StreamReader = New StreamReader(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "yourFile.txt"))
Dim input = "", fields() As String
While (Not reader.EndOfStream)
input = reader.ReadLine()
fields = input.Split(",")
Dim itemToAdd = fields(0).Replace("""", "")
ComboBox.Items.Add(itemToAdd)
End While
I'm having a little trouble with the iTexSharp library for .Net.
I want to generate a QR code and show the resulting image in a web page (or save it into a file, or wathever). Problem is, the class BarcodeQRCode only has the method GetImage() wich returns an iTextShrap.text.image object. From there I've tried to use the RawData property to create a Memory Stream, and that Memory Stream to create a Bitmap, but i get the Invalid Parameter Error. This is the code
1 Dim oQR As New BarcodeQRCode("DATA TO BE ENCODED", 1, 1, Nothing)
2 Dim oMS As New MemoryStream(oQR.GetImage().RawData)
3 Dim oBitmap As New Bitmap(oMS)
4 oBitmap.Save("C:\Users\MyUser\Documents\codigoQR.png", System.Drawing.Imaging.ImageFormat.Png)
(Error in line 3)
So there seems to be a problem with the Byte Array RawData is returning. Does anyone know the correct way to do what i'm trying, or if I'm missing something?
I'm making a registration form with a picture in VB.Net using an Access database. Utilizing the following code, I am getting an error.
Me.Student_instructorTableAdapter.Insert(txtname,txtpass, ***PictureBox1.Image***)
Me.Student_instructorTableAdapter.Fill(Me.DatabaseDataSet4.student_instructor)
MsgBox("Successfully added", MsgBoxStyle.Information)
The PictureBox1.Image is saving into an Access Database ("picture") which throws an error:
"System.Drawing.Image" cannot be converted to '1-dimensional array"
What should I use instead of PictureBox1.Image when I call Insert in order to avoid the exception?
(Should be a comment but I need to post code)
We need the full exception message to know exactly what it's expecting (1-dimensional array of what?) Most likely it's a Byte array.
If so, you can convert an image to a Byte array like this...
Public Function ImageToByteArray(imageIn As System.Drawing.Image) As Byte()
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Return ms.ToArray()
End Function
You can convert a Byte array back to an image like this...
Public Function byteArrayToImage(byteArrayIn As Byte()) As Image
Dim ms As New MemoryStream(byteArrayIn)
Dim returnImage As Image = Image.FromStream(ms)
Return returnImage
End Function
See here for more information
I'm not familiar with access data types so it may support images directly. If so, you need to make sure that you've got the right data type selected for that column/field.
Incidentally, if you don't have to, don't use Access (or at least the storage engine/JET) for new projects - it's slow, unreliable with more than 10 users and has serious security issues. If you don't want a full-up SQL Server database, consider SQL Compact Edition (CE) or SQL Express.
For some reason my previous question was considered too vague. So let me be more specific.
I have a 2 dimensional array of type single.
I want to serialize it to save in an Access database.
The suggestion was to save it as a Memo field which is fine.
I want to later read the Memo field and deserialize it to retrieve the original array.
I have searched extensively on the web and here and can not find the answer. I believe I am serializing the array correctly but do not know how to deserialize it.
This code appears to work for serializing but I can not figure out how to deserialize:
Dim f As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim ms As New MemoryStream
f.Serialize(ms, arLHS)
Dim byArr As Byte() = ms.ToArray
I then save byArr to the Memo field.
Please provide sample code.
You can deserialize it via base64 converter:
Dim str_b64 As String = Convert.ToBase64String(byArr)
Dim ms2 As New MemoryStream(Convert.FromBase64String(str_b64))
Dim intArr2(,) As Int32 = f.Deserialize(ms2)
This may look somewhat awkward, but it works - tested in a console app in VS 2010.
Credit goes here. Through this link, you can also find the full version of the code to play with.
I changed the data type of the Access field from Memo to OLE Object and that seems to work. When I look at the data in Access it tells me the field is "Long binary data" and I was able to use the following code after reading the record from Access:
Dim f As New System.Runtime.Serialization.Formatters.BinaryFormatter
Dim byArr As Byte()
byArr = DirectCast(dtLHS.Rows(0).Item("LHS"), Byte())
Dim theArrayAsString As String = Convert.ToBase64String(byArr)
Dim ms2 As New MemoryStream(Convert.FromBase64String(theArrayAsString))
Dim newLHS(,) as single = f.Deserialize(ms2)
Is this correct?