Dear Experts,
I am building a program which needs to store an Image (any image type) in SQL Express 2008.
I am able to read, and insert images into SQL Datatable, but I can't seem to update my images.
The SQL DataTable contains columnname "picture" - Varbinary(Max)
I am using a Class that holds Public Values (like FirstName).
In this class 'IdentityPicture' and 'PictureDlgFileName' are declared as Image Type.
I am using a Class that contains all Db-Connectivity in code (Select, Insert and Update Commands) called 'PersonDb'.
In my presentation layer, I placed the retrieved image in a Picturebox as BackgroundImage and I use a .png from my 'My.Resources' when the cell in SQL Column 'Picture' is DBNull.
What I am looking for is the proper SQL Update Command.
Please find below the code that I use:
ClassDb (Database Connectivity Class)
Retrieving and Handling the image after SQL Select Command
If drPerson("Picture") Is DBNull.Value Then
Dim img As Image = My.Resources.anonymous
person.IdentityPicture = img.GetThumbnailImage(150, 150, Nothing, Nothing)
Else
Dim PictureDatatable As New DataTable()
PictureDatatable.Load(drPerson)
Dim row As DataRow = PictureDatatable.Rows(0)
Using ms As New IO.MemoryStream(CType(row("Picture"), Byte()))
Dim img As Image = Image.FromStream(ms)
person.IdentityPicture = img.GetThumbnailImage(150, 150, Nothing, Nothing)
End Using
End If
SQL InsertCommand and transforming of Picture
Reads the Image (PictureDlgFileName) from class 'Person' after a FileOK from the OpenFileDialog then uses .addwithvalue parameter to insert the image into SQL Data column named 'Picture'. This works fine too.
Dim cmdPerson As New SqlCommand(sInsertCommand, conDb)
With cmdPerson.Parameters
.AddWithValue("@Picture", IO.File.ReadAllBytes(Person.PictureDlgFileName))
End With
conDb.Open()
cmdPerson.ExecuteNonQuery()
cmdPerson.CommandText = "SELECT @@IDENTITY"
SQL Update Command (not working)!
Dim sUpdateCommand As String = "UPDATE Persons SET Picture = @Picture WHERE PersonId = @PersonId AND Picture = @OldPicture
Dim cmdPerson As New SqlCommand(sUpdateCommand, conDb)
With cmdPerson.Parameters
'Code ommited for other fields
.AddWithValue("Picture", IO.File.ReadAllBytes(NewPerson.IdentityPicture))
.AddWithValue("OldPicture", IO.File.ReadAllBytes(OldPerson.IdentityPicture))
End With
The IO.File.ReadAllBytes(OldPerson.IdentityPicture) is firing up an error telling me that:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from type 'Bitmap' to type 'String' is not valid.
PresentationLayer Code launching Validation and UPDATE
Form Data Validation and Launching Update or Insert Commands:
If ValidData() Then 'Validates and saves all form Controls.
If Me.NewPerson Then
Me.SetPersonFields(Person)
Person.PersonId = PersonDb.InsertPerson(Person)
Me.Close()
Else
Dim NewPerson As New Person()
Me.SetPersonFields(NewPerson)
If PersonDb.UpdatePerson(NewPerson, Person) Then
Person = NewPerson
Me.Close()
End If
End If
End If
Private Sub SetPersonFields(ByVal Pers As Person) 'Note: Save Person
If Person Is Nothing Then
Dim AddPers As New Person
If pbCusPicture.BackgroundImage Is Nothing Then
AddPers.IdentityPicture = My.Resources.anonymous
Else
AddPers.IdentityPicture = pbCusPicture.BackgroundImage
End If
Pers = AddPers
AddPers = Nothing
Else
If pbCusPicture.BackgroundImage Is Nothing Then
Pers.IdentityPicture = My.Resources.anonymous
Else : Pers.IdentityPicture = pbCusPicture.BackgroundImage
End If
End Sub
Please help me - I am staring myself blind on this.
Thank you in advance.