Solved

Committing dataset changes to the database

Posted on 2004-09-22
11
349 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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