[thelist] ASP.NET 2.0 (VB): SqlDataAdapter.Update(DataTable) trouble
Casey Crookston
caseyb at thecrookstons.com
Thu Jan 31 21:35:23 CST 2008
Hi, and thanks in advance for your help!
>From a hign level, here's what I'm trying to do. I have a flat comma
delimited file (the source) that I need to import into a database table (the
destination), but not until after the
source data is massaged and mingled with destination data. So, I import the
flat-file source-data into a first DataTable using a first DataAdapter.
Then, I use a second DataAdapter to import the current destination data into
a second DataTable. Then I do the logic and place the resulting data into
the second dataset.
So far so good. Now, all I have left to do is update the destination
database table with the new data. Easy, right? But I can't make it happen. I
know it's a lot of code, but here's what I've got:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Create connection to the flat file & load data into a data table
Dim TextConn As New
System.Data.OleDb.OleDbConnection(WebConfigurationManager.ConnectionStrings("CJ_DATA").ConnectionString)
Dim dtblSource As DataTable = New DataTable
dad1.Fill(dtblSource)
' Verify that the data is corect by binding to a gridview for visual display
GridView1.DataSource = dtblSource
GridView1.DataBind()
' Now, get the currentdata from the destination database table
Dim dbConn As New
SqlConnection(WebConfigurationManager.ConnectionStrings("GCC_Remote").ConnectionString)
Dim dad2 As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM
import_temp_cj2", dbConn)
Dim dtblDestination As DataTable = New DataTable
dad2.Fill(dtblDestination)
' verify the data is correct by binding to a second gridview for display
GridView2.DataSource = dtblDestination
GridView2.DataBind()
' Create Update, Insert, and Delete commands
Dim builder As New SqlCommandBuilder(dad2)
' merge the two datasets
' /* code removed for easy reading */
' verify the new data is correct by binding to a third gridview for display
GridView3.DataSource = dtblDestination
GridView3.DataBind()
dad2.UpdateBatchSize = 0
Dim numUpdates As Integer = dad2.Update(dtblDestination)
label1.Text = "Rows updated: " & numUpdates.ToString
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As a tutorial, I have been using Sam's Asp.Net Unleashed 2.0 (page 834) and
this example:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqlcommandbuilderclasstopic.asp
>From everything I can see, there should be NO REASON why it is not working.
All three datagrids display exactly what I would expect them to display with
no error is returned at runtime. But, the data in the database table
remains unchanged, and the label at the end always returns zero. I think I
am overlooking something simple, but I sure can't find it. If you can spot
my error, it sure would make me happy!
Thanks,
Casey
More information about the thelist
mailing list