954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Saving Multiple Pictures

I have to save 4 different pictures in my database. NSO, GoodMoral, ReportCard & TOR.
Here is my code.

Sub savepic()

        Dim str As String = "My connection"
        Dim con As New SqlClient.SqlConnection

        con.ConnectionString = str
        Dim ms As New IO.MemoryStream()

        nsopic.Image.Save(ms, nsopic.Image.RawFormat)
        goodmoralpic.Image.Save(ms, goodmoralpic.Image.RawFormat)
        repcardpic.Image.Save(ms, repcardpic.Image.RawFormat)
        torpic.Image.Save(ms, torpic.Image.RawFormat)

        Dim arrimage() As Byte = ms.GetBuffer

        Dim cmd As New SqlCommand("insert into store_image_requirements (NSOBirthC,ReportCard,GoodMoral,TOR,Student_No,Surname,First_Name,Middle_Name)values(@picture1,@picture2,@picture3,@picture4, '" & txtstud_no.Text & "', '" & txtsurname.Text & "', '" & txtfirstname.Text & "', '" & txtmiddlename.Text & "')", con)
        
        cmd.Parameters.Add(New SqlParameter("@picture", SqlDbType.Image)).Value = arrimage

        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()


My problem is, it allows me to look for different picture then save it without error. But when I check on the database(MSSQL) it doesn't save the different pictures but one picture for all four requirements (NSO, GoodMoral, ReportCard & TOR). How can I be able to save multiple images in my database? Please help. Thank You

aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

Help :(

aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

Or maybe anyone can teach me how to save the picture in a folder instead?

aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

use this code to save pic

'use this code instead of this code (        cmd.Parameters.Add(New SqlParameter("@picture", SqlDbType.Image)).Value = arrimage)
   Dim myFile As String = picture
            If picture.ToString.Length <= 1 Then
                Me.myCmd.Parameters.Add("@Picture", SqlDbType.Image).Value = Convert.DBNull
            Else
                Dim myStream As FileStream = New FileStream(myFile, FileMode.Open)
                Dim myImageBuffer() As Byte = New Byte(myStream.Length) {}
                myStream.Read(myImageBuffer, 0, myStream.Length)

                Me.myCmd.Parameters.Add("@Picture", SqlDbType.Image).Value = myImageBuffer
                myStream.Close()
            End If


regards

waqasaslammmeo
Posting Pro in Training
472 posts since Aug 2011
Reputation Points: 38
Solved Threads: 82
 

The query syntax to insert a picture into SQL can be found here

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

there's no problem with saving the picture. the only problem is that it doesn't save the pictures i selected for nso,goodmoral,reportcard and tor. instead it saves the same picture for the four requirements which is wrong bec i selected diff pictures for each

aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

I already found the solution. Thanks for the help! :)

aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

Can you please post the solution you found? It may be of help to someone else.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

This is what I did :)

Dim str As String = "Data Source=CAMILLEPC;Initial Catalog=WAIS.mdf;Integrated Security=True"
Dim con As New SqlClient.SqlConnection

con.ConnectionString = str

Dim ms As New IO.MemoryStream()
Dim ms2 As New IO.MemoryStream()
Dim ms3 As New IO.MemoryStream()
Dim ms4 As New IO.MemoryStream()

nsopic.Image.Save(ms, nsopic.Image.RawFormat)
goodmoralpic.Image.Save(ms2, goodmoralpic.Image.RawFormat)
repcardpic.Image.Save(ms3, repcardpic.Image.RawFormat)
torpic.Image.Save(ms4, torpic.Image.RawFormat)

Dim arrimage() As Byte = ms.GetBuffer
Dim arrimage2() As Byte = ms2.GetBuffer
Dim arrimage3() As Byte = ms3.GetBuffer
Dim arrimage4() As Byte = ms4.GetBuffer

Dim cmd As New SqlCommand("insert into store_image_requirements (NSOBirthC,ReportCard,GoodMoral,TOR,NSOBirthCLabel,ReportCardLabel,GoodMoralLabel,TORLabel,Student_No,Surname,First_Name,Middle_Name)values(@picture1,@picture2,@picture3,@picture4, '" & PathNSO.Text & "', '" & PathRC.Text & "', '" & PathGM.Text & "', '" & PathTOR.Text & "', '" & txtstud_no.Text & "', '" & txtsurname.Text & "', '" & txtfirstname.Text & "', '" & txtmiddlename.Text & "')", con)

cmd.Parameters.Add(New SqlParameter("@picture1", SqlDbType.Image)).Value = arrimage
cmd.Parameters.Add(New SqlParameter("@picture2", SqlDbType.Image)).Value = arrimage2
cmd.Parameters.Add(New SqlParameter("@picture3", SqlDbType.Image)).Value = arrimage3
cmd.Parameters.Add(New SqlParameter("@picture4", SqlDbType.Image)).Value = arrimage4

MessageBox.Show("Scanned Requirements Saved!", "Save Digital Requirements", MessageBoxButtons.OK, MessageBoxIcon.Information)

con.Open()
cmd.ExecuteNonQuery()
con.Close()

aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: