Solved

DataAdapter not updating SQL Server table

Posted on 2009-04-09
3
234 Views
Last Modified: 2012-05-06
I have a routine in which I am attempting to loop through a text file using a StreamReader and import the data I mine from it to a SQL Server 2008 table called BSR.  I can populate an entire dataset with no problems (checked the dataset contents in debug mode and all is well) but it will not update to SQL Server no matter what I try.  

The dataset (ds) is of type dsBSR, which is a drag-and-drop from Server Explorer of the table BSR.  This is my first attempt at typed datasets and so far I am failing miserably.  The code is pasted below, and all it needs to do is add newly imported lines (which are in a table in the dataset) to the table in SQL Server.  Thanks in advance for your assistance!
Dim da As New SqlDataAdapter("SELECT * FROM BSR", con)
        Dim con As New SqlConnection(My.Settings.BSROnSQLServer)
        Dim ds As New dsBSR
        Dim row As dsBSR.BSRRow
 
...        
 
        Try
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
 
            'Code to populate dataset is in this range
 
            ds.AcceptChanges()
            da.FillSchema(ds, SchemaType.Mapped, "BSR")
            Dim cb As New SqlCommandBuilder(da)
            da.InsertCommand = cb.GetInsertCommand
            da.UpdateCommand = cb.GetUpdateCommand
            MessageBox.Show(da.Update(ds, "BSR") & " records imported.", "Import results")
        Catch dbex As SqlException
            MessageBox.Show("Error: " & dbex.ToString)
        Finally
            con.Close()
        End Try
        ...

Open in new window

0
Comment
Question by:pigparent
[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
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 200 total points
ID: 24107815
I think the call to AcceptChanges might be part of the problem. According to the documentation:

"When you call AcceptChanges on the DataSet, any DataRow objects still in edit-mode end their edits successfully. The RowState property of each DataRow also changes; Added and Modified rows become Unchanged, and Deleted rows are removed."

That means when you call Update and the framework looks at what it has to do it finds all the rows are in an Unchanged state and says, "Nothing to do here."
0
 
LVL 1

Author Closing Comment

by:pigparent
ID: 31568536
Thank you, thank you, THANK YOU!
0
 
LVL 1

Author Comment

by:pigparent
ID: 24107885
Thanks so much!  I meant to mention in my question that I was grasping at straws, trying everything I found on the net to try to make this work.  Your solution made my day!
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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 …

729 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