Trouble in saving a longblob image to database - vb.net

I want to get the image from the datagridview then save it to the database. Can anyone help me with this? It will be so much appreciated
Below is my code in saving the image in the database. I don't know why but it doesn't actually save anything.
Try
connectionSync()
Dim a, b As String
Dim Sql = "INSERT INTO SAMPLE (ID, IMG)values(#a,#b)"
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
a = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()
Dim cmd As New MySqlCommand(Sql, ConSync)
Dim memorystream1 As New MemoryStream()
Dim filename As String = DataGridView1.Rows(i).Cells(1).Value
Dim bitmaps As New Bitmap(filename)
bitmaps.Save(memorystream1, Imaging.ImageFormat.Jpeg)
Dim pic() As Byte = memorystream1.GetBuffer()
cmd.Parameters.AddWithValue("#a", a)
cmd.Parameters.AddWithValue("#b", bitmaps)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
ConSync.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

Related

Insert & receive picture from ms access query db vb.net

so, I want to insert a picture to my ms-access DB and it works fine.
inserting code :
Dim ms As New System.IO.MemoryStream
Dim bmpImage As New Bitmap(GunaPictureBox1.Image)
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
bytImage = ms.ToArray()
ms.Close()
Dim d As New DBConnect
d.Editdata(String.Format("INSERT INTO TeachersTable (TeacherFN, TeacherLN, [TeacherImage]) values('{0}','{1}','{2}')", GunaTextBox1.Text, GunaTextBox2.Text, bytImage))
display code :
Dim dt As DataTable = New DBConnect().selectdata(String.Format("SELECT [TeachersTable.TeacherImage] FROM TeachersTable where TeachersTable.ID= {0}", 1))
Dim EditTeacher As New EditTeacher
Dim pictureData As Byte() = DirectCast(dt.Rows(0)(1), Byte())
Dim stream As New IO.MemoryStream(pictureData)
EditTeacher.GunaPictureBox1.Image = Image.FromStream(stream)
EditTeacher.Show()
but now I want to display it on a picture box but when I execute the code its shows me an error said "Parameters is not valid" & I don't know where is the problem!

Vb.net the process cannot access the file because it is being used by another process

Hello i wanted to open an csv file and write text in it.
Here is the code:
Try
Dim path As String = "C:\Users\daaemoon\Desktop\Test.csv"
Dim reader As StreamReader = New System.IO.StreamReader(File.OpenRead(path))
Dim sw As New StreamWriter(path)
Dim s As String = String.Empty
While reader.Peek() >= 0
Dim line As String = reader.ReadLine()
Dim values As String() = line.Split(";"c)
listforfalsecsv.Add(values(0))
s = s + line + Chr(10)
End While
reader.Close()
sw.Write(s)
sw.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
i got the error by the following line:
Dim sw As New StreamWriter(path)
If i use the method reader.dispose() after that line Dim reader As StreamReader = New System.IO.StreamReader(File.OpenRead(path))
i come to the while loop, but than i got an error: From an closen Textreader could not been read.
I need help, please help me.

Trouble in converting an image from database to byte and back to image

I have a button and a datagridview in my form. I need to get the image from the database and display its value in the datagrid then get the value of the image in the datagrid to copy it to another database.
Specifically, I'm copying the image (blob) from table1 of database1 to table 1 of database2.
Button1_click:
Dim img As Image
Dim bArr As Byte()
Try
Dim Sql = "Select ID, IMG from sample"
connectionOnline()
Dim cmd = New MySqlCommand(Sql, ConOnline)
Dim dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
DataGridView1.Rows.Clear()
While dr.Read = True
img = dr(1)
bArr = imgToByteArray(img)
DataGridView1.Rows.Add(dr(0), bArr)
End While
ConOnline.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
connectionSync()
Dim a, b As String
Dim Sql = "INSERT INTO SAMPLE (ID, IMG)values(#a,#b)"
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
a = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()
Dim img1 As Image = byteArrayToImage(bArr)
Dim cmd As New MySqlCommand(Sql, ConSync)
cmd.Parameters.AddWithValue("#a", a)
cmd.Parameters.AddWithValue("#b", img1)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
ConSync.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
connectionSync()
Dim Sql = "INSERT INTO B.SAMPLE(ID, IMG) SELECT ID, IMG FROM C.SAMPLE WHERE not exists (SELECT 1 from B.SAMPLE WHERE B.SAMPLE.ID=C.SAMPLE.ID)"
Dim cmd = New MySqlCommand(Sql, ConSync)
With cmd
.ExecuteNonQuery()
End With
MsgBox("Success", vbInformation, "Save")
ConSync.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
connectionOffline()
Dim Sql = "UPDATE SAMPLE SET IMG=(SELECT C.SAMPLE.NAME FROM C.SAMPLE WHERE C.SAMPLE.ID=B.SAMPLE.ID) WHERE B.SAMPLE.ID=(SELECT C.SAMPLE.ID FROM C.SAMPLE WHERE C.SAMPLE.ID=B.SAMPLE.ID)"
Dim cmd = New MySqlCommand(Sql, ConOffline)
With cmd
.ExecuteNonQuery()
End With
MsgBox("Success")
ConOffline.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
B and C are databases
While sample is the table
below are the functions i used to convert the image
Public Function imgToByteArray(ByVal img As Image) As Byte()
Using mStream As New MemoryStream()
img.Save(mStream, img.RawFormat)
Return mStream.ToArray()
End Using
End Function
Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
Using mStream As New MemoryStream(byteArrayIn)
Return Image.FromStream(mStream)
End Using
End Function
The result (in the database; IMG) only shows: "System.Drawing.Bitmap" instead of the actual image
You are storing only a "Image.ToString".
Try to use a Binary parameter:
Dim yourParam As new SqlParameter("#b", System.Data.SqlDbType.Binary)
yourParam.Direction = ParameterDirection.Input
' yourParam.value= yourImage ' you can't place a system.drawin.image here
Dim ms As New MemoryStream()
yourImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
yourParam.value= ms.ToArray() ' similar to your imgToByteArray function ....
cmd.Parameters.Add(yourParam)

Digital Person fingerprint image retrieving

I am using the following code to save the finger print scanned through Digital Persona's device.
Dim cls As New ClsDataAccess
Dim con = New SqlConnection(cls.SqlConnectiontring)
Dim cmd As New SqlCommand
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
Dim serializedTemplate As Byte() = str.ToArray()
'cmd.Parameters.Add(New SqlParameter("#fn", SqlDbType.VarChar, 10)).Value = "Joe"
'sql.DbType
Dim param(0) As SqlParameter
'Dim t As Integer = Join(serializedTemplate, ",")
param(0) = New SqlParameter("#biometricData", serializedTemplate)
'Public OnlineConnectionString As String = "Data Source = 203.234.5.678; Database = mydb; User CndID = username; Password = xxxxxx;"
'Dim cmd As New SqlCommand("Insert Into tbltestbio (biovalue) Values (#biometricData)", con)
'cmd.Parameters.Add(param)
Dim pictureParameter As SqlClient.SqlParameter = New SqlClient.SqlParameter("#Picture", SqlDbType.Binary)
pictureParameter.Value = serializedTemplate
cmd.Parameters.Add(pictureParameter)
The problem is that when I try to retrieve the image using memory stream like an ordinary image, it won't load. Any other image using my Object Browser is being displayed by this code. What am I doing wrong? Below is the code for image retrieval.
Sub ImageLoadFun(ByVal barrImg() As Byte)
Try
Dim ms As New MemoryStream(barrImg)
Dim returnImage As Image = Image.FromStream(ms)
PictureBox1.Image = returnImage.Image
EmployeeDrawPicture(img)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
It seems you get "template" from sensor and not "image".
Template is a list of minutiae and other info.
You need to check in your FingerPrint API if there's method to get image.
Be careful, usually image, from fingerprint sensor, is not in RGB format but in greyscale.

The process cannot access the file 'file.csv' because it is being used by another process

I have a code which process each file in a folder and send the data in the file to SQL Database and then deletes the file. But often i get this Exception "The process cannot access the file 'file.csv' because it is being used by another process." Please can anyone point me out in the right direction, will really appreciate that.
The code is shown below :
Dim dirinfo As DirectoryInfo
Dim allFiles() As FileInfo
dirinfo = New DirectoryInfo("E:\SQLUPDATE\CAC")
allFiles = dirinfo.GetFiles("*.csv")
If allFiles.Length <> 0 Then
Try
For Each fl As FileInfo In allFiles
'MsgBox(fl.FullName.ToString())
Dim con As SqlConnection = New SqlConnection(SQL_con2)
Dim sr As StreamReader = New StreamReader(fl.FullName)
Dim line As String = sr.ReadLine
Dim value() As String = line.Split(Microsoft.VisualBasic.ChrW(44))
Dim dt As DataTable = New DataTable
Dim row As DataRow
For Each dc As String In value
dt.Columns.Add(New DataColumn(dc))
Next
While Not sr.EndOfStream
value = sr.ReadLine.Split(Microsoft.VisualBasic.ChrW(44))
If (value.Length = dt.Columns.Count) Then
row = dt.NewRow
row.ItemArray = value
dt.Rows.Add(row)
End If
End While
Dim bc As SqlBulkCopy = New SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock)
bc.DestinationTableName = "[DB].[dbo].[CAC_LData]"
bc.BatchSize = dt.Rows.Count
con.Open()
bc.WriteToServer(dt)
bc.Close()
con.Close()
sr.Close()
System.IO.File.Delete(fl.FullName)
sr.Dispose()
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If