locked
Updating access database directly from within gridview RRS feed

  • Question

  • Hi All,

    Just starting out creating a database and struggling with the following:-

    I have a gridview bound to an access database table called users. My question is how do you update the database on the fly when the user changes the data withinthe gridview. I believe currently this is only updating the dataset, not the master access database.
    Your help is very much appreciated!
    Tuesday, November 13, 2007 10:16 PM

Answers

  • Do you use a DataAdapter to fill your dataset?

    Use the Update method, passing the DataSet as parameter. This saves the DataSet changes to your database.

    To accomplish this task, you need to set the UpdateCommand and DeleteCommand (if need) properties.

     

    Code Block
    MyTableAdapter.Update(MyDataGridViewDS)

     

     

     

    Regards,

    Tuesday, November 13, 2007 10:43 PM
  • Springs,

     

    Please change the parameter of DataAdapter.Update method to the instance name of your DataSet in order to update your DataSet and return the updated rows.

     

    In your code snippet, you just shows that the parameter is the instance of DataGridView, that is not correct. Please change to DataSet name just like this:

     

    UsersTableAdapter.Update(yourDataset)

     

    The following code snippet shwos you an example on updating the data source:

     

    Code Block

        Public Function CreateCommandAndUpdate( _

        ByVal connectionString As String, _

        ByVal queryString As String) As DataSet

     

            Dim dataSet As DataSet = New DataSet

     

            Using connection As New OleDbConnection(connectionString)

                connection.Open()

                Dim adapter As New OleDbDataAdapter()

     

                adapter.SelectCommand = New OleDbCommand( _

                    queryString, connection)

     

                Dim builder As OleDbCommandBuilder = _

                    New OleDbCommandBuilder(adapter)

     

                adapter.Fill(dataSet)

     

                ' Code to modify the data in the DataSet here.

     

                ' Without the OleDbCommandBuilder this line would fail.

                builder.GetUpdateCommand()

                adapter.Update(dataSet)

            End Using

            Return dataSet

        End Function

     

     

     

    For further information on the issue, please take a look at the following link in MSDN on DataAdapter.Update method:

     

    http://msdn2.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx 

     

    Hope that can help you.

    Friday, November 16, 2007 6:17 AM

All replies

  • Do you use a DataAdapter to fill your dataset?

    Use the Update method, passing the DataSet as parameter. This saves the DataSet changes to your database.

    To accomplish this task, you need to set the UpdateCommand and DeleteCommand (if need) properties.

     

    Code Block
    MyTableAdapter.Update(MyDataGridViewDS)

     

     

     

    Regards,

    Tuesday, November 13, 2007 10:43 PM
  • Thanks for your reply. I am using a dataAdapter to fill my dataset.

    I put the above code on a commandbutton click event and get an error.....any ideas??

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          UsersTableAdapter.Update(DataGridView1)
    End Sub
    Tuesday, November 13, 2007 10:52 PM
  • Springs,

     

    Please change the parameter of DataAdapter.Update method to the instance name of your DataSet in order to update your DataSet and return the updated rows.

     

    In your code snippet, you just shows that the parameter is the instance of DataGridView, that is not correct. Please change to DataSet name just like this:

     

    UsersTableAdapter.Update(yourDataset)

     

    The following code snippet shwos you an example on updating the data source:

     

    Code Block

        Public Function CreateCommandAndUpdate( _

        ByVal connectionString As String, _

        ByVal queryString As String) As DataSet

     

            Dim dataSet As DataSet = New DataSet

     

            Using connection As New OleDbConnection(connectionString)

                connection.Open()

                Dim adapter As New OleDbDataAdapter()

     

                adapter.SelectCommand = New OleDbCommand( _

                    queryString, connection)

     

                Dim builder As OleDbCommandBuilder = _

                    New OleDbCommandBuilder(adapter)

     

                adapter.Fill(dataSet)

     

                ' Code to modify the data in the DataSet here.

     

                ' Without the OleDbCommandBuilder this line would fail.

                builder.GetUpdateCommand()

                adapter.Update(dataSet)

            End Using

            Return dataSet

        End Function

     

     

     

    For further information on the issue, please take a look at the following link in MSDN on DataAdapter.Update method:

     

    http://msdn2.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx 

     

    Hope that can help you.

    Friday, November 16, 2007 6:17 AM