?
Solved

How do I use SqlDataAdapter.Update to work properly

Posted on 2009-04-10
5
Medium Priority
?
198 Views
Last Modified: 2013-11-08
THis is my first attempt to use the Update method of SqlDataAdapter.    I've found several examples on the web but am unable to get my code to work.     The current version is, probably, a bad combination of examples.

No error is generated.  But the SQL Server table is never updated.
The code does retrieve the needed information.  The for loop works fine.   The SendNotification() function returns true.

Any thought on what might be wrong?
Thank you.
Dim sqlSelect As String = "SELECT [uid] " & _
        "      ,[epk] " & _
        "      ,[user_id] " & _
        "      ,[password] " & _
        "      ,[firstName] " & _
        "      ,[lastName] " & _
        "      ,[email] " & _
        "      ,[genDate] " & _
        "      ,[userOrigin] " & _
        "      ,[sendNotification] " & _
        "      ,[emailTemplate] " & _
        "      ,[snapShotGuid] " & _
        "      ,[NotificationSentOn] " & _
        "  FROM [bbprocessing].[dbo].[UrNewUsers] " & _
        "  WHERE [snapShotGuid] = @snapShotGuid" & _
        "       AND [sendNotification]=1 " & _
        "       AND [NotificationSentOn] < @NotificationSentOn"
 
        Dim sqlUpdate As String = "UPDATE [bbprocessing].[dbo].[UrNewUsers] " & _
            "   SET [sendNotification] = 0 " & _
            "      ,[NotificationSentOn] = @NotificationSentOn " & _
            " WHERE [uid] = @uid "
 
        Dim cn As New SqlConnection
        cn = clsMakeCn.newProc
        Dim da As New SqlDataAdapter(sqlSelect, cn)
 
        With da.SelectCommand.Parameters
            .Clear()
            .AddWithValue("@snapShotGuid", GuidToProcess)
            .AddWithValue("@NotificationSentOn", New DateTime(2000, 1, 1))
        End With
 
        Dim updateCmd As New SqlCommand(sqlUpdate, cn)
        da.UpdateCommand = updateCmd
        da.AcceptChangesDuringUpdate = True
 
        Dim oDataSet As New DataSet
        Try
            da.Fill(oDataSet, "Notifications")
        Catch ex As Exception
            Dim emsg As String = ex.Message
        End Try
 
        For Each dr As DataRow In oDataSet.Tables("Notifications").Rows
            If SendNotification(dr) Then
                Try
                    With da.UpdateCommand.Parameters
                        .Clear()
                        .AddWithValue("@NotificationSentOn", Now)
                        .AddWithValue("@SendNotification", 0)
                        .AddWithValue("@uid", dr("uid"))
                    End With
                    da.UpdateCommand.ExecuteNonQuery()
                Catch ex As Exception
                    Dim eMsg As String = ex.Message
                End Try
            Else
                Dim eMsg As String = "Unable to Send Notification"
            End If
        Next

Open in new window

0
Comment
Question by:RichardKline
[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
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 2000 total points
ID: 24115206
You shouldn't be calling ExecuteNonQuery on the UpdateCommand, what you need to call is the Update method on the DataAdapter.

You also haven't mapped any of the SqlParameters in the update command to the dataset:
' After declaring the Update command
' I've made some assumption about the datatypes used
Dim updateCmd As New SqlCommand(sqlUpdate, cn)
updateCmd.Parameters.Add("NotificationSentOn", DbType.DateTime).SourceColumn = "NotificationSentOn";
updateCmd.Parameters.Add("uid", DbType.Int).SourceColumn = "uid";
 
' Continue with your code
da.UpdateCommand = updateCmd
da.AcceptChangesDuringUpdate = True
 
'With da.UpdateCommand.Parameters
'  .Clear()
'  .AddWithValue("@NotificationSentOn", Now)
'  .AddWithValue("@SendNotification", 0)
'  .AddWithValue("@uid", dr("uid"))
'End With
'da.UpdateCommand.ExecuteNonQuery()
Dim changed As Integer = da.Update(oDataSet)

Open in new window

0
 
LVL 1

Author Comment

by:RichardKline
ID: 24115341
Thank you.

I had to make a couple of small changes to avoid errors  with UpdateCmd.Parameters add lines and Dim changed As Integer = da.Update(oDataSet, "Notifications")

So the code operates without error
    but there is still no change in the SQL table values.



        Dim cn As New SqlConnection
        cn = clsMakeCn.newProc
        Dim da As New SqlDataAdapter(sqlSelect, cn)
 
        With da.SelectCommand.Parameters
            .Clear()
            .AddWithValue("@snapShotGuid", GuidToProcess)
            .AddWithValue("@NotificationSentOn", New DateTime(2000, 1, 1))
        End With
 
        Dim updateCmd As New SqlCommand(sqlUpdate, cn)
        With updateCmd.Parameters
            .Add("NotificationSentOn", SqlDbType.DateTime).SourceColumn = "NotificationSentOn"
            .Add("uid", SqlDbType.Int).SourceColumn = "uid"
        End With
 
        da.UpdateCommand = updateCmd
        da.AcceptChangesDuringUpdate = True
 
        Dim oDataSet As New DataSet
        Try
            da.Fill(oDataSet, "Notifications")
        Catch ex As Exception
            Dim emsg As String = ex.Message
        End Try
 
        For Each dr As DataRow In oDataSet.Tables("Notifications").Rows
            If SendNotification(dr) Then
                Try
                    Dim changed As Integer = da.Update(oDataSet, "Notifications")
                Catch ex As Exception
                    Dim eMsg As String = ex.Message
                End Try
            Else
                Dim eMsg As String = "Unable to Send Notification"
            End If
        Next

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24115394
Can you add a breakpoint to the da.Update line, and check the value of dr.RowState. The Update method will only execute the Update command on rows that have a RowState of Modified.
0
 
LVL 1

Author Comment

by:RichardKline
ID: 24115417
Found it.  Silly omission.  Neglected to se the new values before updating.
                dr("SendNotification") = 0
                dr("NotificationSentOn") = Now

The complete and working new code is below.
        Dim sqlSelect As String = "SELECT [uid] " & _
        "      ,[epk] " & _
        "      ,[user_id] " & _
        "      ,[password] " & _
        "      ,[firstName] " & _
        "      ,[lastName] " & _
        "      ,[email] " & _
        "      ,[genDate] " & _
        "      ,[userOrigin] " & _
        "      ,[sendNotification] " & _
        "      ,[emailTemplate] " & _
        "      ,[snapShotGuid] " & _
        "      ,[NotificationSentOn] " & _
        "  FROM [bbprocessing].[dbo].[UrNewUsers] " & _
        "  WHERE [snapShotGuid] = @snapShotGuid" & _
        "       AND [sendNotification]=1 " & _
        "       AND [NotificationSentOn] < @NotificationSentOn"
 
        Dim sqlUpdate As String = "UPDATE [bbprocessing].[dbo].[UrNewUsers] " & _
            "   SET [sendNotification] = 0 " & _
            "      ,[NotificationSentOn] = @NotificationSentOn " & _
            " WHERE [uid] = @uid "
 
        Dim cn As New SqlConnection
        cn = clsMakeCn.newProc
        Dim da As New SqlDataAdapter(sqlSelect, cn)
 
        With da.SelectCommand.Parameters
            .Clear()
            .AddWithValue("@snapShotGuid", GuidToProcess)
            .AddWithValue("@NotificationSentOn", New DateTime(2000, 1, 1))
        End With
 
        Dim updateCmd As New SqlCommand(sqlUpdate, cn)
        With updateCmd.Parameters
            .Clear()
            .Add("NotificationSentOn", SqlDbType.DateTime).SourceColumn = "NotificationSentOn"
            .Add("uid", SqlDbType.Int).SourceColumn = "uid"
        End With
 
        da.UpdateCommand = updateCmd
        da.AcceptChangesDuringUpdate = True
 
        Dim oDataSet As New DataSet
        Try
            da.Fill(oDataSet, "Notifications")
        Catch ex As Exception
            Dim emsg As String = ex.Message
        End Try
 
        For Each dr As DataRow In oDataSet.Tables("Notifications").Rows
            If SendNotification(dr) Then
                dr("SendNotification") = 0
                dr("NotificationSentOn") = Now
                Try
                    Dim changed As Integer = da.Update(oDataSet, "Notifications")
                Catch ex As Exception
                    Dim eMsg As String = ex.Message
                End Try
            Else
                Dim eMsg As String = "Unable to Send Notification"
            End If
        Next

Open in new window

0
 
LVL 1

Author Comment

by:RichardKline
ID: 24115568
Also neglected to say "Thanks!"   I appreciate the Excellent help.  
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 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