Click here to Skip to main content
15,886,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

I m trying to update 2 images in the same form using 2 picture boxes and 2 dialog boxes
, I can update only the first image using PictureBox1 but unfortunately i can't see any changes applied to the second one ! .

I m using the following code :-


 updateQuery = " UPDATE Student, Relatives , Relatives_Student , Gardian_Relation SET Student_First_Name = '" & txt_F_name.Text _
            & "', Student_Middle_Name = '" & txt_M_name.Text _
            & "', Student_Third_Name = '" & txt_T_name.Text _
            & "', Student_Family_Name = '" & txt_Fm_name.Text _
            & "', Student_DOB = '" & txt_DOB_Date.Text _
            & "', Student_Photo = @Student_Photo" _
            & "', Student_DOB_Cert = @Student_DOB_Cert" _
            & "', Student_Gender = '" & strGender _
            & "', Student_Nationality = '" & strCountry _
            & "', Student_Enroll_Date = '" & txt_En_Date.Text _
            & "' WHERE Student_ID  = '" & intMySelectedCell _
            & "' AND Student.Student_ID = Relatives_Student.rel_Student_ID AND Relatives.Relative_ID = Relatives_Student.rel_Relative_ID AND Relatives.Gardiant_Relation_ID = Gardian_Relation.Gardian_ID  AND gardian_relation.Gardian_ID = '" & intGardian_ID & "'"



        Dim command As New MySqlCommand(updateQuery, connection)
    

        With command 


            Dim bm As Bitmap = New Bitmap(PictureBox1.Image)
            Dim bm2 As Bitmap = New Bitmap(PictureBox2.Image)

            bm.Save(mstream, PictureBox1.Image.RawFormat)
            bm2.Save(mstream2, PictureBox2.Image.RawFormat)

            Dim arrPic() As Byte = mstream.GetBuffer()
            .Parameters.AddWithValue("@Student_Photo", arrPic)


            Dim arrPic2() As Byte = mstream2.GetBuffer()
            .Parameters.AddWithValue("@Student_DOB_Cert", arrPic2)


            .ExecuteNonQuery()

        End With

Any help is extremely appritiated.
Kind regards
Salim

What I have tried:

I have success to update only the first Image
Posted
Updated 1-Sep-19 8:24am

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Comments
Salim Al-Adawi 1-Sep-19 12:45pm    
Thank you for your comments i have really benefited . Although i m still waiting for
answer regarding the core question.
I have solved the problem using the following code :

cmd = con.CreateCommand()
cmd.CommandText = "UPDATE staff_hr SET Staff_Name=@name,Staff_Nationality_ID=@nation_id,Staff_Contact_No=@contact,Staff_EMail=@email,Staff_Salary=@salary,Staff_DOB=@dob,Staff_Join_Date=@doj,Staff_Age=@age,Staff_City_ID=@cityid,Staff_Village_ID=@villageid,Staff_photo=@StaffPhoto,Staff_Cert=@Staff_Cert WHERE Staff_id=@id;"
cmd.Parameters.AddWithValue("@id", intStaff_ID)
cmd.Parameters.AddWithValue("@name", txt_Staff_Name.Text)
cmd.Parameters.AddWithValue("@nation_id", intNationalityID)
cmd.Parameters.AddWithValue("@contact", txt_Staff_Contact_No.Text)
cmd.Parameters.AddWithValue("@email", txt_Staff_EMail.Text)
cmd.Parameters.AddWithValue("@salary", txt_Staff_Salary.Text)
cmd.Parameters.AddWithValue("@dob", Date.ParseExact(txt_Staff_DOB.Text.Trim, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture))
cmd.Parameters.AddWithValue("@doj", Date.ParseExact(txt_Staff_Join_Date.Text.Trim, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture))
cmd.Parameters.AddWithValue("@age", txt_Age.Text)
cmd.Parameters.AddWithValue("@cityid", intCityID)
cmd.Parameters.AddWithValue("@villageid", intVillageID)


'Get and save the image

With cmd
If opn_File_IMG.FileName = Nothing Then ' Image in the Image File Dialogue

Dim row As DataRow = dt.Rows(0)

Using mstream As New IO.MemoryStream(CType(row(0), Byte()))

Dim img As Image = Image.FromStream(mstream)
pic_Staff_Photo.Image = img

.Parameters.Add(New MySql.Data.MySqlClient.MySqlParameter("@StaffPhoto", MySql.Data.MySqlClient.MySqlDbType.LongBlob)).Value = (CType(row(0), Byte())) 'Get current existing image in database if PictureBox does not have an image'

End Using
Else
.Parameters.Add(New MySql.Data.MySqlClient.MySqlParameter("@StaffPhoto", MySql.Data.MySqlClient.MySqlDbType.LongBlob)).Value = IO.File.ReadAllBytes(opn_File_IMG.FileName) ' Get user selected image
End If

If OpenFileDialog1.FileName = Nothing Then ' Image in the Image File Dialogue

Dim row As DataRow = dt.Rows(0)

Using mstream As New IO.MemoryStream(CType(row(0), Byte()))

Dim img As Image = Image.FromStream(mstream)
PictureBox2.Image = img

.Parameters.Add(New MySql.Data.MySqlClient.MySqlParameter("@Staff_Cert", MySql.Data.MySqlClient.MySqlDbType.LongBlob)).Value = (CType(row(0), Byte())) 'Get current existing image in database if PictureBox does not have an image'

End Using
Else
.Parameters.Add(New MySql.Data.MySqlClient.MySqlParameter("@Staff_Cert", MySql.Data.MySqlClient.MySqlDbType.LongBlob)).Value = IO.File.ReadAllBytes(OpenFileDialog1.FileName) ' Get user selected image
End If

.ExecuteNonQuery()

End With
 
Share this answer
 

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