mySQL update error. D:[Solved]
So I have this code:
[php]
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports MySql.Data.MySqlClient
Public Class Form1
Dim conn As MySqlConnection
Dim data As DataTable
Dim da As MySqlDataAdapter
Dim cb As MySqlCommandBuilder
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not conn Is Nothing Then conn.Close()
Dim connStr As String
connStr = String.Format("server=" & TextBox1.Text & ";user id=" & TextBox4.Text & "; password=" & TextBox5.Text & "; database=" & TextBox2.Text & "; pooling=false", _
TextBox1.Text, TextBox4.Text, TextBox5.Text)
Try
conn = New MySqlConnection(connStr)
conn.Open()
ShowTbl()
Catch ex As MySqlException
MessageBox.Show("Error connecting to the server: " + ex.Message)
End Try
End Sub
Private Sub ShowTbl()
data = New DataTable
da = New MySqlDataAdapter("SELECT * FROM " + TextBox3.Text, conn)
cb = New MySqlCommandBuilder(da)
da.Fill(data)
DataGridView1.DataSource = data
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim changes As DataTable = data.GetChanges()
da.Update(changes)
data.AcceptChanges()
Catch ex as Exception
msgbox(ex.tostring())
End Try
End Sub
End Class
[/php]
And I get errors when i do:
[php]
da.Update(changes)
[/php]
Errormsg:
[php]
---------------------------
Add Product
---------------------------
MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from the stream has failed. ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
at MySql.Data.MySqlClient.MySqlStream.ReadFully(Strea m stream, Byte[] buffer, Int32 offset, Int32 count)
at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int3 2& affectedRow, Int32& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult( )
at MySql.Data.MySqlClient.MySqlDataReader.NextResult( )
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader( CommandBehavior behavior)
at System.Dat*****mmon.DbDataAdapter.UpdatingRowStatus Errors(RowUpdatingEventArgs rowUpdatedEvent, DataRow dataRow)
at System.Dat*****mmon.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at MySql.Data.MySqlClient.MySqlDataAdapter.Update(Dat aRow[] dataRows, DataTableMapping tableMapping)
at System.Dat*****mmon.DbDataAdapter.UpdateFromDataTab le(DataTable dataTable, DataTableMapping tableMapping)
at System.Dat*****mmon.DbDataAdapter.Update(DataTable dataTable)
at Add_Product.Form1.Button2_Click(Object sender, EventArgs e) in C:\Users\Win7Sig PC\Documents\Visual Studio 2010\Projects\Add Product\Add Product\Form1.vb:line 43
---------------------------
OK
---------------------------
[/php]
how to fix? I am dumbfounded
Ok I fixed it
/request close
The problem was the table didn't know where the table changes started so it would be null from the start, and steam reader would go past the reading area. To fix it you need to define changes when the table loads so:
Instead of:
[php]
Private Sub ShowTbl()
data = New DataTable
da = New MySqlDataAdapter("SELECT * FROM " + TextBox3.Text, conn)
cb = New MySqlCommandBuilder(da)
da.Fill(data)
DataGridView1.DataSource = data
End Sub
[/php]
Define a table after load with this:
Code:
Private Sub ShowTbl()
data = New DataTable
da = New MySqlDataAdapter("SELECT * FROM " + TextBox3.Text, conn)
cb = New MySqlCommandBuilder(da)
da.Fill(data)
DataGridView1.DataSource = data
data.AcceptChanges()
End Sub