Click here to Skip to main content
15,886,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i'm having a little problem here.

First when my form is loaded it gets information from database (ms accsses)
But if i make some changes in textboxes and click save button it doesn't read the picturebox that it has image on it.

Because when form loads it loads the image in the Picturebox, then i just wanted to edit some text and click save and it will not allow me , because the picturebox is empty for him.

Lets start with the read information:
VB
Try
            With cmd
                Dim stream As New IO.MemoryStream()
                conn.Open()
                .Connection = conn
                .CommandText = "select cPicture from Table where ID=@uID"
                .Parameters.Add("@uID", OleDbType.Integer, 50).Value = TextBox6.Text
                Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                stream.Write(image, 0, image.Length)
                Dim bitmap As New Bitmap(stream)
                PictureBox1.Image = bitmap '--->I have used another picturebox to display image from database.
                stream.Close()
                .Parameters.Clear()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cmd.Dispose()
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try


Here will display me the picture inside the picturebox (result is okay)
Now for save :
VB
Try
            With cmd
                Dim ms As New IO.MemoryStream()
                PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
                Dim arrimage() As Byte = ms.GetBuffer

                If Not PictureBox1.Image Is Nothing Then
                    MsgBox("missing")
                Else
                    MsgBox("has")
                End If
                conn.Open()
                .Connection = conn
                .CommandText = "UPDATE Table SET cName = @uName,cNumber = @uNumber,cSupplier = @uSupp,cStore = @uStore,cCount = @uCount,cPicture = @picture WHERE ID = 1"
                '.Parameters.Add("@uID", OleDbType.Integer).Value = TextBox6.Text
                .Parameters.Add("@uName", OleDbType.VarChar, 50).Value = TextBox1.Text
                .Parameters.Add("@uNumber", OleDbType.BigInt, 50).Value = TextBox2.Text
                .Parameters.Add("@uSupp", OleDbType.VarChar, 50).Value = TextBox3.Text
                .Parameters.Add("@uStore", OleDbType.VarChar, 50).Value = TextBox4.Text
                .Parameters.Add("@uCount", OleDbType.Integer, 50).Value = TextBox5.Text
                .Parameters.Add("@Picture", OleDbType.Binary).Value = arrimage
                .ExecuteNonQuery()
                .Parameters.Clear()
                ms.Close()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cmd.Dispose()
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try


And here gives me the message:
MsgBox("missing")


And query required 6 stuff to make the change.
How to solve it?

What I have tried:

don't know exactly where the problem comes
Posted
Updated 8-Jun-20 2:37am
v2
Comments
[no name] 7-Jun-20 6:43am    
Should this
If Not PictureBox1.Image Is Nothing Then
not be like this
If PictureBox1.Image Is Nothing Then ?
diablo22 7-Jun-20 6:48am    
yes, my mistake in copy here, but its just for test i add this to see..that it doesnt recognize to have picture.
The problem comes when execute the query, it doesn run it because it expect 6 arguments
[no name] 7-Jun-20 6:57am    
"it doesn run it" means what? Any error/exception message?
Richard MacCutchan 7-Jun-20 6:57am    
You have added uID as the first parameter in your set, but that is not in your INSERT command. If the id column is declared as auto-generated then you should not try to add it manually. If it is not auto generated then you need to include it in the INSERT statement.
[no name] 7-Jun-20 6:58am    
uID is commented out. And the command is UPDATE

.Parameters.Add("@uName", OleDbType.VarChar, 50).Value = TextBox1.Text
                .Parameters.Add("@uNumber", OleDbType.Integer, 50).Value = TextBox2.Text
                .Parameters.Add("@uSupp", OleDbType.VarChar, 50).Value = TextBox3.Text
                .Parameters.Add("@uStore", OleDbType.VarChar, 50).Value = TextBox4.Text
                .Parameters.Add("@uCount", OleDbType.Integer, 50).Value = TextBox5.Text
                .Parameters.Add("@Picture", OleDbType.Binary).Value = arrimage
                .Parameters.AddWithValue("@uID", TextBox6.Text)
.CommandText = "UPDATE Connectors SET cName = @uName,cNumber= @uNumber,cSupplier = @uSupp,cStore = @uStore,cCount = @uCount,cPicture = @Picture WHERE ID = @uID"
                .ExecuteNonQuery()


I got it Parameters are problems, but also the order should be like this to work correct.
 
Share this answer
 
Comments
[no name] 7-Jun-20 10:07am    
Confusing, I don't get the point why this should work now...
diablo22 7-Jun-20 10:13am    
i readed about a little and also number order matter in this call, also the calls must before the command to get the names, also i fixed the parameters that makes problems too to database , now they are equal and correct too
diablo22 wrote in a comment:
i readed about a little and also number order matter in this call, also the calls must before the command to get the names, also i fixed the parameters that makes problems too to database , now they are equal and correct too
The order of parameters doesn't matter at all when they are named.

However, you appear to be using OleDbCommand parameters.
The documentation states:
The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used.
See OleDbCommand.Parameters Property (System.Data.OleDb) | Microsoft Docs[^]
So your command text should be
VB
CommandText = "UPDATE Table SET cName = ?,cNumber = ?,cSupplier = ?,cStore = ?,cCount = ?,cPicture = ? WHERE ID = 1"
 
Share this answer
 
Comments
diablo22 8-Jun-20 10:46am    
i also readed about ? and @ and both methods works pretty fine for that and for me @ is much better to make the code

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