Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

       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!

Posted

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..

 

don't I feel foolish :/

Posted

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

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...