We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,140 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

3
Contributors
8
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
9
Views
Question
Answered
aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

Help :(

aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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
Skill Endorsements: 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

M.Waqas Aslam
Master Poster
745 posts since Aug 2011
Reputation Points: 50
Solved Threads: 121
Skill Endorsements: 2

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

Reverend Jim
Carpe per diem
Moderator
3,604 posts since Aug 2010
Reputation Points: 561
Solved Threads: 448
Skill Endorsements: 32

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
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Reverend Jim and M.Waqas Aslam

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
Skill Endorsements: 0

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

Reverend Jim
Carpe per diem
Moderator
3,604 posts since Aug 2010
Reputation Points: 561
Solved Threads: 448
Skill Endorsements: 32

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
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0857 seconds using 2.72MB