Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Committing dataset changes to the database

Posted on 2004-09-22
11
Medium Priority
?
363 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

618 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