I keep getting this error when saving image to mysql db :/

trend

Well-known member
Joined
Oct 12, 2004
Messages
171
Hello, I am trying to adapt MS code:
http://support.microsoft.com/default.aspx?scid=kb;en-us;308042
to store a picture in a mysql database. I keep getting this error:

An unhandled exception of type System.InvalidOperationException occurred in system.data.dll

Additional information: Update requires a valid InsertCommand when passed DataRow collection with new rows.

and it selects this line: "da.Update(ds, "MyImages")"

here is my code.

Code:
        Dim con As MySqlConnection
        Dim connString As String = "Server=192.168.254.7;username=me;password=nopeeking;database=test;persist security info=true;compress=true;pooling=true"
        con = New MySqlConnection(connString)
        Dim MyCommand As String = "Select * From MyImages"
        Dim da As New MySqlDataAdapter(MyCommand, con)


        Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
        Dim ds As New DataSet

        da.MissingSchemaAction = MissingSchemaAction.AddWithKey

        Dim fs As New FileStream("C:\windows\Gone Fishing.BMP", FileMode.OpenOrCreate, FileAccess.Read)
        Dim MyData(fs.Length) As Byte
        fs.Read(MyData, 0, fs.Length)
        fs.Close()
        con.Open()
        da.Fill(ds, "MyImages")
        Dim myRow As DataRow
        myRow = ds.Tables("MyImages").NewRow()

        myRow("Description") = "This would be description text"
        myRow("imgField") = MyData
        ds.Tables("MyImages").Rows.Add(myRow)
        da.Update(ds, "MyImages")

        fs = Nothing
        MyCB = Nothing
        ds = Nothing
        da = Nothing

        con.Close()
        con = Nothing
        MsgBox("Image saved to database")



Any ideas?

I would definitly be up for diff code.. as I have to populate a database with tons of pictures.. and this code will take forever to upload all the pics

thanks guys!
 
ok.. well i just rewrote the code using some snippets from the code.. and it works now.. I think the issue was just I declared:
Dim MyCB As mySqlCommandBuilder = New mySqlCommandBuilder(da )

in the wrong place..

dont I feel foolish :/
 
Easy mistake to make. I have had a problem before of dimming my insert statement before one of variables had a value and then when my code went to use the statement, although it was called after my variable had a value, it would still die everytime until I moved my dim statement down. Another question, I am getting into mysql some, what field type are you using in mysql to store the image files?

Chester
 
Back
Top