Problem with DataGridView
-
Hi all,I'm having problem here with my code. I would like to Update the Database with my DataGridView1 when Click the Button1. But from the code below,I found that an Error have occured. Error from Visual Studio 2008= System.Data: Missing the DataColumn 'DO_No'in the DataTable 'Main_table' for the SourceColumn 'DO_No'. The following are the code I load the data from database to my DataGridView1:
Private Sub MaintainDO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ObjMyConn = New MySqlConnection(MyConnString)SQL = "SELECT \* FROM main\_table" Try ObjMyConn.Open() ObjAdapter = New MySqlDataAdapter(SQL, ObjMyConn) ObjComBuilder = New MySqlCommandBuilder(ObjAdapter) ds = New DataSet ObjAdapter.Fill(ds, "Main\_table") dt = ds.Tables("Main\_table") dt.Columns(0).ColumnName = "Date" dt.Columns(1).ColumnName = "DO No." dt.Columns(2).ColumnName = "Lorry No." dt.Columns(3).ColumnName = "Load From" dt.Columns(4).ColumnName = "To Site" dt.Columns(5).ColumnName = "Company Name" dt.Columns(6).ColumnName = "Item Description" DataGridView1.DataSource = dt.DataSet.Tables("Main\_table") DataGridView1.Refresh() Catch ex As Exception MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly) Finally ObjMyConn.Close() End Try End Sub
The code I put inside my Button1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SQL = "SELECT \* FROM main\_table" Try ObjAdapter.Update(ds.Tables("Main\_table")) '<< Here is how I used to update my database. ds.Tables("Main\_table").Clear() DataGridView1.DataSource = Nothing DataGridView1.Refresh() ObjMyConn.Open() ObjAdapter = New MySqlDataAdapter(SQL, ObjMyConn) ObjComBuilder = New MySqlCommandBuilder(ObjAdapter) ObjAdapter.Fill(ds, "Main\_table") dt = ds.Tables("Main\_table") dt.Columns(0).ColumnName = "Date" dt.Columns(1).ColumnName = "DO No." dt.Columns(2).ColumnName = "Lorry No." dt.Columns(3).ColumnName = "Load From" dt.Columns(4).ColumnName = "To Site"
-
Hi all,I'm having problem here with my code. I would like to Update the Database with my DataGridView1 when Click the Button1. But from the code below,I found that an Error have occured. Error from Visual Studio 2008= System.Data: Missing the DataColumn 'DO_No'in the DataTable 'Main_table' for the SourceColumn 'DO_No'. The following are the code I load the data from database to my DataGridView1:
Private Sub MaintainDO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ObjMyConn = New MySqlConnection(MyConnString)SQL = "SELECT \* FROM main\_table" Try ObjMyConn.Open() ObjAdapter = New MySqlDataAdapter(SQL, ObjMyConn) ObjComBuilder = New MySqlCommandBuilder(ObjAdapter) ds = New DataSet ObjAdapter.Fill(ds, "Main\_table") dt = ds.Tables("Main\_table") dt.Columns(0).ColumnName = "Date" dt.Columns(1).ColumnName = "DO No." dt.Columns(2).ColumnName = "Lorry No." dt.Columns(3).ColumnName = "Load From" dt.Columns(4).ColumnName = "To Site" dt.Columns(5).ColumnName = "Company Name" dt.Columns(6).ColumnName = "Item Description" DataGridView1.DataSource = dt.DataSet.Tables("Main\_table") DataGridView1.Refresh() Catch ex As Exception MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly) Finally ObjMyConn.Close() End Try End Sub
The code I put inside my Button1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SQL = "SELECT \* FROM main\_table" Try ObjAdapter.Update(ds.Tables("Main\_table")) '<< Here is how I used to update my database. ds.Tables("Main\_table").Clear() DataGridView1.DataSource = Nothing DataGridView1.Refresh() ObjMyConn.Open() ObjAdapter = New MySqlDataAdapter(SQL, ObjMyConn) ObjComBuilder = New MySqlCommandBuilder(ObjAdapter) ObjAdapter.Fill(ds, "Main\_table") dt = ds.Tables("Main\_table") dt.Columns(0).ColumnName = "Date" dt.Columns(1).ColumnName = "DO No." dt.Columns(2).ColumnName = "Lorry No." dt.Columns(3).ColumnName = "Load From" dt.Columns(4).ColumnName = "To Site"
OK. I think it's pretty obvious just by reading the error message. You're missing a column in your DataTable("Main_table") that you included in your SQL statement.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
OK. I think it's pretty obvious just by reading the error message. You're missing a column in your DataTable("Main_table") that you included in your SQL statement.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Thanks Mr. Dave,I have solve the problem. I make a mistaken on the DataGridView1.Column1 header name. So I change it to the original name and it works now. Thanks alot to Mr. Dave and everyone reading here. Regards :) Drex