Solved

DataAdapter not updating SQL Server table

Posted on 2009-04-09
3
231 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
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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.
Creating and Managing Databases with phpMyAdmin in cPanel.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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