Problem in OleDbDataAdapter.Update [modified]
-
Hi, I have to do updates to my mdb access database, which has 2 column, user_name and user_pwd. I have followed the suggestion from MSDN, but I don't know it still got error when excuting update().It says "Update command" or "Insert Command" to have connection object... Can someone give me some hints or point out to me that What I missed? Here is my code:
Dim connStr As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\me.mdb") Dim sqlStr As String = "SELECT * FROM wpLogin" Dim dt As New DataTable() Dim dataAdapter As OleDbDataAdapter Dim ODBconn As OleDbConnection Public Function CreateDataAdapter(ByVal selectCommand As String, ByVal connection As OleDbConnection) As OleDbDataAdapter Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(selectCommand, connection) adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ' Create the commands. adapter.InsertCommand = New OleDbCommand( _ "INSERT INTO wpLogin (user_name, user_pwd) " & _ "VALUES (?, ?)") adapter.UpdateCommand = New OleDbCommand( _ "UPDATE wpLogin SET user_name = ?, user_pwd = ? " & _ "WHERE user_name = ?") adapter.DeleteCommand = New OleDbCommand("DELETE FROM wpLogin WHERE user_name = ?") ' Create the parameters. adapter.InsertCommand.Parameters.Add( _ "@user_name", OleDbType.Char, 50, "user_name") adapter.InsertCommand.Parameters.Add( _ "@user_pwd", OleDbType.Char, 50, "user_pwd") adapter.UpdateCommand.Parameters.Add( _ "@user_name", OleDbType.Char, 50, "user_name") adapter.UpdateCommand.Parameters.Add( _ "@user_pwd", OleDbType.Char, 50, "user_pwd") adapter.DeleteCommand.Parameters.Add( _ "@user_name", OleDbType.Char, 50, "user_name").SourceVersion = _ DataRowVersion.Original Return adapter End Function Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click 'Displays the table's data in the data grid 'Clear the current contents of the table dt.Clear() 'Fill the data table with data from the database 'Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr) ODBconn = New OleDbConnection(connStr) dataAdapter = CreateDataAdapter(sqlStr, ODBconn