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

Recommended Answers

All 8 Replies

Help :(

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

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

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

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

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

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()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.