Bound datagrid won't commit changes 
Author Message
 Bound datagrid won't commit changes

Hi there,

I hope I'm missing something obvious here ;-) but the following code meant
to commit the changes in the datagrid and dataset does not work. The
datagrid is filled properly, but closing the form results in the following
error: "System.InvalidOperationException: Update requires a valid
UpdateCommand when passed DataRow collection with modified rows". I am
connecting to a SQL Server 2K database hosted locally and I'm binding the
datagrid at run-time. Here is the test code:

Public Class frmTest
    Inherits System.Windows.Forms.Form

    ...

    Dim objDA As SqlDataAdapter
    Dim objDS As New DataSet()

    Private Sub frmTest_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim objCmd As New SqlCommand("SELECT * FROM tblPrijzen",
g_objSQLServer.Connection)
        objCmd.CommandType = CommandType.Text

        Try
            objDA = New SqlDataAdapter(objCmd)
            objDA.Fill(objDS, "dummy")
        Catch
            '
        End Try

        grdTest.SetDataBinding(objDS, "dummy")
    End Sub

    Private Sub frmTest_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Try
            objDA.Update(objDS, "dummy")
        Catch objExc As Exception
            ' The error is raised here...
            MessageBox.Show(objExc.ToString())
        End Try
    End Sub
End Class



Fri, 01 Apr 2005 23:29:06 GMT  
 Bound datagrid won't commit changes
Hi SLE,

It seems that you are missing the UpdateCommand object. When calling
SqlDataAdapter.Update() method, the DataAdapter examines the RowState
property, and executes the required INSERT, UPDATE, or DELETE statements
iteratively for each row.

You can either create Update, Delete, Insert command objects manually and
associate them to the SqlDataAdapter object or use the SqlCommandBuilder
class to automatically generate these commands.

Hope this helps.

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------

Quote:

>Newsgroups: microsoft.public.dotnet.languages.vb
>Subject: Bound datagrid won't commit changes
>Date: Mon, 14 Oct 2002 17:29:06 +0200
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2720.3000
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
>Lines: 48

>Organization: -= Skynet Usenet Service =-
>NNTP-Posting-Host: 194.78.207.108
>X-Trace: 1034609349 reader1.news.skynet.be 1347 194.78.207.108

>Path:

cpmsftngxa09!cpmsftngxa08!cppssbbsa01.microsoft.com!news-out.cwix.com!newsfe
ed.cwix.com!news.tele.dk!small.news.tele.dk!195.238.3.187!skynet.be!skynet.b
e!louie!not-for-mail
Quote:
>Xref: cpmsftngxa09 microsoft.public.dotnet.languages.vb:69631
>X-Tomcat-NG: microsoft.public.dotnet.languages.vb

>Hi there,

>I hope I'm missing something obvious here ;-) but the following code meant
>to commit the changes in the datagrid and dataset does not work. The
>datagrid is filled properly, but closing the form results in the following
>error: "System.InvalidOperationException: Update requires a valid
>UpdateCommand when passed DataRow collection with modified rows". I am
>connecting to a SQL Server 2K database hosted locally and I'm binding the
>datagrid at run-time. Here is the test code:

>Public Class frmTest
>    Inherits System.Windows.Forms.Form

>    ...

>    Dim objDA As SqlDataAdapter
>    Dim objDS As New DataSet()

>    Private Sub frmTest_Load(ByVal sender As Object, ByVal e As
>System.EventArgs) Handles MyBase.Load
>        Dim objCmd As New SqlCommand("SELECT * FROM tblPrijzen",
>g_objSQLServer.Connection)
>        objCmd.CommandType = CommandType.Text

>        Try
>            objDA = New SqlDataAdapter(objCmd)
>            objDA.Fill(objDS, "dummy")
>        Catch
>            '
>        End Try

>        grdTest.SetDataBinding(objDS, "dummy")
>    End Sub

>    Private Sub frmTest_Closing(ByVal sender As Object, ByVal e As
>System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
>        Try
>            objDA.Update(objDS, "dummy")
>        Catch objExc As Exception
>            ' The error is raised here...
>            MessageBox.Show(objExc.ToString())
>        End Try
>    End Sub
>End Class



Sun, 03 Apr 2005 16:25:02 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Datagrid 6.0 won't commit changes to 7.0 SQL Database

2. Help: Committing changes in DataGrid (bound to access db)

3. How to commit changes in DataGrid?

4. How do I get the DataGrid 6.0 to commit changes to SQL Database

5. OLE Bound control won't bind ADO source

6. OLE Bound control won't bind ADO source

7. OLE Bound control won't bind ADO source

8. How to commit datagrid's current row?

9. Change specific row colors in a bound datagrid

10. Changing data dsiplayed in a bound DataGrid

11. Bound combo box won't let be cancel/undo, continually generating error messages

12. DataGrid: how to bind an item's value

 

 
Powered by phpBB® Forum Software