Solved

Committing dataset changes to the database

Posted on 2004-09-22
11
335 Views
Last Modified: 2008-03-17
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
Comment
Question by:gleznov
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 7

Expert Comment

by:J_Mak
ID: 12129931
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
 
LVL 7

Expert Comment

by:J_Mak
ID: 12129972
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
 
LVL 10

Expert Comment

by:123654789987
ID: 12130294
0
 
LVL 7

Expert Comment

by:J_Mak
ID: 12130314
This is also a very useful link to manipulating datagrids:

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

Cheers!
0
 
LVL 2

Expert Comment

by:ahmadfuwad
ID: 12130398
'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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:gleznov
ID: 12132144
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
 
LVL 2

Expert Comment

by:ahmadfuwad
ID: 12132374
'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
 

Author Comment

by:gleznov
ID: 12132589
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
 
LVL 2

Expert Comment

by:ahmadfuwad
ID: 12140336
'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
 

Author Comment

by:gleznov
ID: 12141789
Gives me an error about using nulls in a non-null field.  What exactly's that code do?

JP
0
 
LVL 2

Accepted Solution

by:
ahmadfuwad earned 250 total points
ID: 12141916
'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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now