dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Saving multiple pictures in DB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    79

    Resolved [RESOLVED] Saving multiple pictures in DB

    Dear all,
    I have in a single form 8 different pictureboxes where I can insert some pictures. Per each of them I have a textbox. Now, if I fill just 2 pictureboxes with two images, I would like to code to save in an SQL Server table just those two images with the related textboxes.

    I wrote the following code:

    Code:
    Dim cmdImm As SqlCommand
            Dim insertImm As String = String.Empty
    
            Dim dbsourceImm As String = "Data Source=.\SQLEXPRESS;Initial Catalog=DBImmaginiPartite;Integrated Security=True;Pooling=False"
            Dim connImm = New SqlConnection(dbsourceImm)
            Dim ms1 As New MemoryStream()
            Dim ms2 As New MemoryStream()
            Dim ms3 As New MemoryStream()
            Dim ms4 As New MemoryStream()
            Dim ms5 As New MemoryStream()
            Dim ms6 As New MemoryStream()
            Dim ms7 As New MemoryStream()
            Dim ms8 As New MemoryStream()
    
            insertImm = "INSERT INTO Tbl_ImmaginiPartite (Codice_partita, Squadra_trasferta, Tipo_azione1, Immagine_azione1, Tipo_azione2, Immagine_azione2, Tipo_azione3, Immagine_azione3, Tipo_azione4, Immagine_azione4, Tipo_azione5, Immagine_azione5, Tipo_azione6, Immagine_azione6, Tipo_azione7, Immagine_azione7, Tipo_azione8, Immagine_azione8) VALUES (@CodParImm, @SOImm, @TA1, @Imm1, @TA2, @Imm2, @TA3, @Imm3, @TA4, @Imm4, @TA5, @Imm5, @TA6, @Imm6, @TA7, @Imm7, @TA8, @Imm8)"
            cmdImm = New SqlCommand
            cmdImm.CommandType = CommandType.Text
            cmdImm.Connection = connImm
    
            connImm.Open()
            cmdImm.CommandText = insertImm
    
            Dim codicePImm As String = TextBox4.Text + TextBox5.Text + TextBox1.Text + TextBox2.Text + TextBox3.Text + DateTimePicker1.Value.ToShortDateString
    
            Try
    
                cmdImm.Parameters.Add("@CodParImm", SqlDbType.NChar).Value = codicePImm
                cmdImm.Parameters.Add("@SOImm", SqlDbType.NChar).Value = TextBox8.Text.Trim
    
                If PictureBox16.Image Is Nothing Then
                    GoTo Immagine2
                Else
                    PictureBox16.Image.Save(ms1, Imaging.ImageFormat.Jpeg)
                    Dim data1 As Byte() = ms1.GetBuffer()
                    Dim p1 As New SqlParameter("@Imm1", SqlDbType.Image)
                    p1.Value = data1
                    cmdImm.Parameters.Add("@TA1", SqlDbType.Text).Value = TextBox234.Text.Trim
                    cmdImm.Parameters.Add(p1)
                End If
    
    Immagine2:
    
                If PictureBox18.Image Is Nothing Then
                    GoTo Immagine3
                Else
                    PictureBox18.Image.Save(ms2, Imaging.ImageFormat.Jpeg)
                    Dim data2 As Byte() = ms2.GetBuffer()
                    Dim p2 As New SqlParameter("@Imm2", SqlDbType.Image)
                    p2.Value = data2
                    cmdImm.Parameters.Add("@TA2", SqlDbType.Text).Value = TextBox236.Text.Trim
                    cmdImm.Parameters.Add(p2)
                End If
    
    Immagine3:
    
                If PictureBox17.Image Is Nothing Then
                    GoTo Immagine4
                Else
                    PictureBox17.Image.Save(ms3, Imaging.ImageFormat.Jpeg)
                    Dim data3 As Byte() = ms3.GetBuffer()
                    Dim p3 As New SqlParameter("@Imm3", SqlDbType.Image)
                    p3.Value = data3
                    cmdImm.Parameters.Add("@TA3", SqlDbType.Text).Value = TextBox235.Text.Trim
                    cmdImm.Parameters.Add(p3)
                End If
    
    Immagine4:
    
                If PictureBox19.Image Is Nothing Then
                    GoTo Immagine5
                Else
                    PictureBox19.Image.Save(ms4, Imaging.ImageFormat.Jpeg)
                    Dim data4 As Byte() = ms4.GetBuffer()
                    Dim p4 As New SqlParameter("@Imm4", SqlDbType.Image)
                    p4.Value = data4
                    cmdImm.Parameters.Add("@TA4", SqlDbType.Text).Value = TextBox237.Text.Trim
                    cmdImm.Parameters.Add(p4)
                End If
    
    Immagine5:
    
                If PictureBox23.Image Is Nothing Then
                    GoTo Immagine6
                Else
                    PictureBox23.Image.Save(ms5, Imaging.ImageFormat.Jpeg)
                    Dim data5 As Byte() = ms5.GetBuffer()
                    Dim p5 As New SqlParameter("@Imm5", SqlDbType.Image)
                    p5.Value = data5
                    cmdImm.Parameters.Add("@TA5", SqlDbType.Text).Value = TextBox238.Text.Trim
                    cmdImm.Parameters.Add(p5)
                End If
    
    Immagine6:
    
                If PictureBox21.Image Is Nothing Then
                    GoTo Immagine7
                Else
                    PictureBox21.Image.Save(ms6, Imaging.ImageFormat.Jpeg)
                    Dim data6 As Byte() = ms6.GetBuffer()
                    Dim p6 As New SqlParameter("@Imm6", SqlDbType.Image)
                    p6.Value = data6
                    cmdImm.Parameters.Add("@TA6", SqlDbType.Text).Value = TextBox240.Text.Trim
                    cmdImm.Parameters.Add(p6)
                End If
    
    Immagine7:
    
                If PictureBox22.Image Is Nothing Then
                    GoTo Immagine8
                Else
                    PictureBox22.Image.Save(ms7, Imaging.ImageFormat.Jpeg)
                    Dim data7 As Byte() = ms7.GetBuffer()
                    Dim p7 As New SqlParameter("@Imm7", SqlDbType.Image)
                    p7.Value = data7
                    cmdImm.Parameters.Add("@TA7", SqlDbType.Text).Value = TextBox239.Text.Trim
                    cmdImm.Parameters.Add(p7)
                End If
    
    Immagine8:
    
                If PictureBox20.Image Is Nothing Then
                    GoTo Messaggio
                Else
                    PictureBox20.Image.Save(ms8, Imaging.ImageFormat.Jpeg)
                    Dim data8 As Byte() = ms8.GetBuffer()
                    Dim p8 As New SqlParameter("@Imm8", SqlDbType.Image)
                    p8.Value = data8
                    cmdImm.Parameters.Add("@TA8", SqlDbType.Text).Value = TextBox241.Text.Trim
                    cmdImm.Parameters.Add(p8)
                End If
    
    
    
            Catch ex As System.Data.SqlClient.SqlException
                MsgBox(ex.Message)
    
            End Try
    
    Messaggio:
    
            MsgBox("Immagini salvate")
    
            cmdImm.ExecuteNonQuery()
            connImm.Close()
        End Sub
    Now, if execute the code inserting in the form just one picture, I get an error in relation to parameter @TA2 which is not initialized. I thought that the check on the pictureboxes should avoid this, but in fact it is not.

    How can I change the code so that I don't need to fill all the textboxes and all the pictureboxes to make it working?

    Thanks,
    A.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,098

    Re: Saving multiple pictures in DB

    First of all an unrelated issue... there is no need for the GoTo's (they just make the code harder to read, and add more text - they give no benefit whatsoever). Instead of this:
    Code:
                If PictureBox16.Image Is Nothing Then
                    GoTo Immagine2
                Else
                    ...
                End If
    Immagine2:
    ...you could do this:
    Code:
                If PictureBox16.Image Is Nothing Then
                Else
                    ...
                End If
    ...or just this:
    Code:
                If PictureBox16.Image IsNot Nothing Then
                    ...
                End If

    As to the issue, if you have parameter placeholders (like @TA1) in your SQL statement, you must also add the parameter to go along with it. You can however set it to be empty by using DBNull.Value, eg:
    Code:
                    Dim p1 As New SqlParameter("@Imm1", SqlDbType.Image)
                    p1.Value = DBNull.Value
    To avoid repetition of code, you can take some of it out of the If/Else, eg:
    Code:
                Dim p1 As New SqlParameter("@Imm1", SqlDbType.Image)
                If PictureBox16.Image Is Nothing Then
                    p1.Value = DBNull.Value
                    cmdImm.Parameters.Add("@TA1", SqlDbType.Text).Value = DBNull.Value
                Else
                    PictureBox16.Image.Save(ms1, Imaging.ImageFormat.Jpeg)
                    Dim data1 As Byte() = ms1.GetBuffer()
                    p1.Value = data1
                    cmdImm.Parameters.Add("@TA1", SqlDbType.Text).Value = TextBox234.Text.Trim
                End If
                cmdImm.Parameters.Add(p1)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    79

    Re: Saving multiple pictures in DB

    Dear si_the_geek,
    thanks a lot for your prompt suggestion: it worked very well!

    Thanks again,
    A.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width