Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using vb net 2017.

By this next code i get request IMAGE from sql table (filed name MMOBJS.LNL_BLOB)

VB
     OpenConnection()

     cmd = New SqlCommand("Select badge.ID , emp.SSNO , emp.LASTNAME, emp.FIRSTNAME, emp.MIDNAME, BADGSTAT.NAME, DEPT.NAME , BADGE.ACTIVATE, BADGE.DEACTIVATE, MMOBJS.LNL_BLOB  FROM EMP INNER JOIN BADGE ON EMP.ID = BADGE.EMPID INNER JOIN UDFEMP ON UDFEMP.ID = EMP.ID INNER JOIN DEPT on UDFEMP.DEPT = DEPT.ID INNER JOIN BADGSTAT ON BADGE.STATUS = BADGSTAT.ID INNER JOIN MMOBJS ON MMOBJS.EMPID = BADGE.EMPID where BADGE.ID Like '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "%'", connections)
reader = cmd.ExecuteReader()


What I have tried:

and by next code, i can save one image for one selected record.

VB
If reader.Read = False Then
     MessageBox.Show("Фото для пропуска № " & DataGridView1.CurrentRow.Cells(0).Value.ToString & " нет в базе.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else

     'store image value from the database in byte
     Dim imageData As Byte() = reader(9)
     If Not imageData Is Nothing Then
         Using ms As New MemoryStream(imageData, 0, imageData.Length)
             ms.Write(imageData, 0, imageData.Length)
             pic_box.Image = Image.FromStream(ms, True)

             Dim Path As String = "C:\tmp2\"
             Dim Dir As String = System.IO.Path.GetDirectoryName(Path)

             Try
                 Try
                     If Not System.IO.Directory.Exists(Dir) Then
                         System.IO.Directory.CreateDirectory(Dir)
                     End If

                     With sfdpic
                         .Title = "Save image As"
                         .Filter = "Jpg, Jpeg Images|*.jpg; *.jpeg"
                         .AddExtension = True
                         .DefaultExt = ".jpg"
                         '.FileName = txt_ssno.Text + " " + txt_lname.Text + " " + txt_fname.Text + " " + txt_midname.Text
                         .FileName = DataGridView1.CurrentRow.Cells(1).Value.ToString + " " + DataGridView1.CurrentRow.Cells(2).Value.ToString + " " + DataGridView1.CurrentRow.Cells(3).Value.ToString + " " + DataGridView1.CurrentRow.Cells(4).Value.ToString
                         .OverwritePrompt = True
                         .InitialDirectory = Dir
                         .RestoreDirectory = True

                         If .ShowDialog = DialogResult.OK Then
                             If .FilterIndex = 1 Then
                                 pic_box.Image.Save(sfdpic.FileName, Imaging.ImageFormat.Jpeg)
                             End If
                         End If
                     End With

                 Catch ex As Exception
                     MessageBox.Show("Error: Saving Image Failed ->>" & ex.Message.ToString())

                 Finally
                     sfdpic.Dispose()
                 End Try

             Catch ex As Exception
                 Me.Cursor = Cursors.Default
                 MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
             End Try

         End Using
     End If
     reader.Close()
     Cursor = Cursors.Default
 End If
 Cursor = Cursors.Default

 Try



I cannot save multiple image for filtered datagridview list. How can i do it?
Any example please.
Posted
Updated 13-Aug-18 10:11am
v3

1 solution

I noticed two errors right away.
Unfortunately VB is not my primary language and I do not have access to an IDE at the moment. You are going to need to work out any errors in syntax on your own.

Problem #1: Don't ever concatenate an SQL command together.
Use Parameterized queries to remove chances of SQL Injection
Fix: Change the WHERE clause
VB
' BAD:
     "...where BADGE.ID Like '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "%'"

' Good:
     "...where BADGE.ID = @BadgeID"
     cmd.Parameters.AddWithValue("@BadgeID", DataGridView1.CurrentRow.Cells(0).Value);


Problem #2: Only 1 value retrieved
Fix: Loop through the reader

VB
If reader.HasRows = False Then
	MessageBox.Show("Error Message")
Else
   While reader.Read()
      ' do your code here for each row in the reader object
   End While
End If
 
Share this answer
 
Comments
Roman Kuznetsov 14-Aug-18 7:54am    
Hello, thanks for your advise, it's very helpfull for me. I am chandged my code for Problem #1 , it's worked.

But cannot rewrite my code for saving multiple photo from db. Can you help me with any example? My task is to saving all photo from filtered dgv table to separate jpg file , without using SafeFileDialog.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900