?
Solved

Committing dataset changes to the database

Posted on 2004-09-22
11
Medium Priority
?
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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
 

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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 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