[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

Committing dataset changes to the database

Hello,

     I have a form that takes a table from a database as a dataset.  Then, the user changes any or all cells in any row of their choice.  They can also delete or add rows.  When changing cells in a row, they may change all cells in a row and that row still need to overwrite the original in the database, even though there are no remaining fields that are the same.  

     I don't understand how to commit my dataset's changes into the actual database.  I'm using sqladapter for this.  Any help's appreciated!

JP
0
gleznov
Asked:
gleznov
  • 4
  • 3
  • 3
  • +1
1 Solution
 
J_MakCommented:
Try this link:

http://visualbasic.about.com/library/weekly/aa052403a.htm

It has a step by step process of doing what you want.

This is also a great link:

http://www.dotnetspider.com/Technology/Tutorials/DatabaseAccess.aspx
0
 
J_MakCommented:
Another great site that you could use to your advantage is this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp

Seems like a very helpful page!
0
 
123654789987Commented:
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
J_MakCommented:
This is also a very useful link to manipulating datagrids:

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#Top

Cheers!
0
 
ahmadfuwadCommented:
'write following code for update and add row
    Dim cm As SqlCommandBuilder = New SqlCommandBuilder(objDA) 'objDA is dataadapter
        objDA.Update(objDS, "Employee") 'objDS is dataset and employee is your dataset table
                                                          'name remember your table must have primary key
0
 
gleznovAuthor Commented:
ahmadfuwad - I tried the code, and while it didn't cause an error, once it runs, and then I close the form and reopen it (and it reads once more from the database), the new records are gone, the updated records are reverted.

Everybody else - I'm checking links out now...

JP
0
 
ahmadfuwadCommented:
'try this for add and make sure your table must have primary key there is no bug in previous
'code i dont know why it is not working may be one problem is that your table dont have
'primary key thats why it is not working

        Dim row As DataRow
        Dim idx As Integer
        idx = Me.DataGrid1.CurrentRowIndex
        ods.Tables(0).Rows.Add(row)
        row(0) = Me.DataGrid1.Item(idx, 0)
        row(1) = Me.DataGrid1.Item(idx, 1)

'code for delete
        Dim idx As Integer
        idx = Me.DataGrid1.CurrentRowIndex
        ods.Tables(0).Rows(idx).Delete()
0
 
gleznovAuthor Commented:
Actually I didn't have a primary key set, so I went to sqlserver and set primary key to User.

Now the original code crashes my program...

The new one does too:  Row argument cannot be null  - it shouldn't be!

If I change the code to:
       Dim row As DataRow
        Dim idx As Integer
        idx = Me.DBGrid1.CurrentRowIndex
        'row(0) = Me.DBGrid1.Item(idx, 0)
        row(1) = Me.DBGrid1.Item(idx, 1)
        DataSet_Users1.Tables("Users").Rows.Add(row)
I get:
Object reference not set to an instance of an object

JP
0
 
ahmadfuwadCommented:
'try this it will work fine

 Dim row As DataRow = ods.Tables(0).NewRow
        Dim idx As Integer
        idx = Me.DataGrid1.CurrentRowIndex

        ods.Tables(0).Rows(ods.Tables(0).Rows.Count - 1).Item(0) = Me.DataGrid1.Item(idx, 0).ToString
        ods.Tables(0).Rows(ods.Tables(0).Rows.Count - 1).Item(1) = Me.DataGrid1.Item(idx, 1).ToString
        ods.Tables(0).Rows.Add(row)

        Dim cm As SqlCommandBuilder = New SqlCommandBuilder(oda1) 'oDA1 is dataadapter
        oda1.Update(ods, "Customer") 'oDS is dataset and customer is your dataset table
0
 
gleznovAuthor Commented:
Gives me an error about using nulls in a non-null field.  What exactly's that code do?

JP
0
 
ahmadfuwadCommented:
'try this it will solve your problem

Dim row As DataRow = ods.Tables(0).NewRow
        Dim idx As Integer
        idx = Me.DataGrid1.CurrentRowIndex
        If Not IsDBNull(Me.DataGrid1.Item(idx, 0)) Then
            ods.Tables(0).Rows(ods.Tables(0).Rows.Count - 1).Item(0) = Me.DataGrid1.Item(idx, 0).ToString
        End If
        If Not IsDBNull(Me.DataGrid1.Item(idx, 1)) Then
            ods.Tables(0).Rows(ods.Tables(0).Rows.Count - 1).Item(1) = Me.DataGrid1.Item(idx, 1).ToString
        End If
        ods.Tables(0).Rows.Add(row)

        Dim cm As SqlCommandBuilder = New SqlCommandBuilder(oda1) 'oDA1 is dataadapter
        oda1.Update(ods, "Customer") 'oDS is dataset and customer is your dataset table
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now