lijunguo
asked on
Dataset update not updating DB.
Hi,
I'm running an example to use Dataset update function to update DB, but it's not working. therre is no exception, and the dataset's datatable has been uupdated, but not the real SQL DB. I checked the priviledge, it's no problem. Any ideas?
part of the code:
Try
SqlDataAdapter_local.Selec tCommand.C ommandText = "select * from tbl_adsl_signup"
SqlDataAdapter_local.Selec tCommand.C ommandType = CommandType.Text
SqlDataAdapter_local.Selec tCommand.C onnection = SqlConnection_local
SqlConnection_local.Open()
SqlDataAdapter_local.Fill( DataSet_lo cal, "tbl_adsl_signup")
' Create command builder. This line automatically generates the update commands for you, so you don't
' have to provide or create your own.
Dim myDataRowsCommandBuilder As SqlCommandBuilder = New SqlCommandBuilder(SqlDataA dapter_loc al)
' Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
' key & unique key information to be retrieved unless AddWithKey is specified.
SqlDataAdapter_local.Missi ngSchemaAc tion = MissingSchemaAction.AddWit hKey
' SqlDataAdapter_local.Updat eCommand.C onnection( ) = SqlConnection_local
Dim myDataRow, localDataRow As DataRow
Dim loop_int As Integer = 0
For Each myDataRow In DataSet_remote.Tables("tbl _adsl_sign up").Rows
If (loop_int >= local_int(i)) Then
DataSet_local.Tables("tbl_ adsl_signu p").Import Row(myData Row)
End If
loop_int += 1
Next
'SqlConnection_local.Open( )
' Update Database with SqlDataAdapter
SqlDataAdapter_local.Updat e(DataSet_ local.Tabl es("tbl_ad sl_signup" ))
SqlConnection_local.Close( )
Catch ex As Exception
Console.WriteLine(ex.ToStr ing())
End Try
here is the full code.
Imports System.Data.SqlClient
Public Class f_update
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Label1 As System.Web.UI.WebControls. Label
Protected WithEvents get_data As System.Web.UI.WebControls. ImageButto n
Protected WithEvents message As System.Web.UI.WebControls. Label
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim SqlDataAdapter_remote As SqlDataAdapter
Dim SqlDataAdapter_local As SqlDataAdapter
Dim SqlConnection_remote As SqlConnection
Dim SqlConnection_local As SqlConnection
Dim DataSet_remote As DataSet
Dim DataSet_local As DataSet
Dim local_int(7) As Integer
Dim remote_int(7) As Integer
Dim outParameter As SqlClient.SqlParameter
Dim i As Integer = 0
Dim spCommand() As String = {"get_adsl_total", "get_apac_total", "get_dial_total", "get_dpac_total", "get_tele_total", "get_dler_total", "get_csid_total"}
Dim sParameter() As String = {"@adsl_intl", "@apac_intl", "@dial_intl", "@dpac_intl", "@tele_intl", "@dler_intl", "@csid_intl"}
Dim tbl_name() As String = {"tbl_adsl_signup", "tbl_adsl_pack", "tbl_dialup_signup", "tbl_dialup_pack", "tbl_telephony_signup", "tbl_dealer_signup", "tbl_customer_id"}
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim adsl_intr, apac_intr, dial_intr, dpac_intr, tele_intr, dler_intr, csid_intr As Integer
'Dim adsl_intl, apac_intl, dial_intl, dpac_intl, tele_intl, dler_intl, csid_intl As Integer
SqlConnection_remote = New SqlConnection("server=(loc al);databa se=test2; UID=lijunguo;PWD=lijunguo" )
SqlConnection_local = New SqlConnection("server=(loc al);databa se=test; UID=lijunguo;PWD=lijunguo" )
SqlDataAdapter_remote = New SqlDataAdapter("select * from customers", SqlConnection_remote)
SqlDataAdapter_local = New SqlDataAdapter("select * from customers", SqlConnection_remote)
DataSet_remote = New DataSet
DataSet_local = New DataSet
'open connection first
If Not Page.IsPostBack Then
SqlConnection_remote.Open( )
SqlDataAdapter_remote.Sele ctCommand. Connection = SqlConnection_remote
'retrieve updated tbl_customer_id from remote server
SqlDataAdapter_remote.Sele ctCommand. CommandTex t = "select customerid, available, producttype from tbl_customer_id where available='no'"
SqlDataAdapter_remote.Fill (DataSet_r emote, "tbl_customer_id")
'retrieve tbl_adsl_signup from remote server
SqlDataAdapter_remote.Sele ctCommand. CommandTex t = "select * from tbl_adsl_signup order by [date] "
SqlDataAdapter_remote.Fill (DataSet_r emote, "tbl_adsl_signup")
'retrieve tbl_adsl_pack from remote server
SqlDataAdapter_remote.Sele ctCommand. CommandTex t = "select * from tbl_adsl_pack order by [date]"
SqlDataAdapter_remote.Fill (DataSet_r emote, "tbl_adsl_pack")
'retrieve tbl_dialup_signup from remote server
SqlDataAdapter_remote.Sele ctCommand. CommandTex t = "select * from tbl_dialup_signup order by [date]"
SqlDataAdapter_remote.Fill (DataSet_r emote, "tbl_dialup_signup")
'retrieve tbl_dialup_pack from remote server
SqlDataAdapter_remote.Sele ctCommand. CommandTex t = "select * from tbl_dialup_pack order by [date]"
SqlDataAdapter_remote.Fill (DataSet_r emote, "tbl_dialup_pack")
'retrieve tbl_dealer_signup from remote server
SqlDataAdapter_remote.Sele ctCommand. CommandTex t = "select * from tbl_dealer_signup order by [date]"
SqlDataAdapter_remote.Fill (DataSet_r emote, "tbl_dealer_signup")
'retrieve tbl_telephony_signup from remote server
SqlDataAdapter_remote.Sele ctCommand. CommandTex t = "select * from tbl_telephony_signup order by [date]"
SqlDataAdapter_remote.Fill (DataSet_r emote, "tbl_telephony_signup")
SqlConnection_remote.Close ()
SqlConnection_local.Open()
'SqlDataAdapter_local.Sele ctCommand = new
SqlDataAdapter_local.Selec tCommand.C onnection = SqlConnection_local
'create output parameter to store value return by SP.
Do While i < 7
outParameter = SqlDataAdapter_local.Selec tCommand.C reateParam eter()
outParameter.ParameterName = sParameter(i)
outParameter.Direction = ParameterDirection.Output
outParameter.SqlDbType = SqlDbType.BigInt
SqlDataAdapter_local.Selec tCommand.C ommandText = spCommand(i)
SqlDataAdapter_local.Selec tCommand.C ommandType = CommandType.StoredProcedur e
SqlDataAdapter_local.Selec tCommand.P arameters. Clear()
SqlDataAdapter_local.Selec tCommand.P arameters. Add(outPar ameter)
'SqlDataAdapter_local.Sele ctCommand. Parameters .Add(New SqlParameter("@adsl_intl", SqlDbType.BigInt))
SqlDataAdapter_local.Selec tCommand.E xecuteNonQ uery()
'SqlDataAdapter_local.Fill (DataSet_l ocal, "tbl_adsl_signup")
If (IsDBNull(SqlDataAdapter_l ocal.Selec tCommand.P arameters( sParameter (i)).Value )) Then
local_int(i) = 0
Else
local_int(i) = SqlDataAdapter_local.Selec tCommand.P arameters( sParameter (i)).Value
End If
i += 1
Loop
SqlConnection_local.Close( )
i = 0
'reset i to 0
Do While i < 6 ' not include total users
remote_int(i) = DataSet_remote.Tables(tbl_ name(i)).R ows.Count
'DataSet_remote.Tables(tbl _name(i)). Rows().
If remote_int(i) > local_int(i) Then
Dim j As Integer 'j as no. of new users
Dim insert_cmd As Integer
j = remote_int(i) - local_int(i)
Select Case i
Case 0
Label1.Text = "You have " & j.ToString() & " new ADSL customers; "
Try
SqlDataAdapter_local.Selec tCommand.C ommandText = "select * from tbl_adsl_signup"
SqlDataAdapter_local.Selec tCommand.C ommandType = CommandType.Text
SqlDataAdapter_local.Selec tCommand.C onnection = SqlConnection_local
SqlConnection_local.Open()
SqlDataAdapter_local.Fill( DataSet_lo cal, "tbl_adsl_signup")
' Create command builder. This line automatically generates the update commands for you, so you don't
' have to provide or create your own.
Dim myDataRowsCommandBuilder As SqlCommandBuilder = New SqlCommandBuilder(SqlDataA dapter_loc al)
' Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
' key & unique key information to be retrieved unless AddWithKey is specified.
SqlDataAdapter_local.Missi ngSchemaAc tion = MissingSchemaAction.AddWit hKey
' SqlDataAdapter_local.Updat eCommand.C onnection( ) = SqlConnection_local
Dim myDataRow, localDataRow As DataRow
Dim loop_int As Integer = 0
For Each myDataRow In DataSet_remote.Tables("tbl _adsl_sign up").Rows
If (loop_int >= local_int(i)) Then
DataSet_local.Tables("tbl_ adsl_signu p").Import Row(myData Row)
End If
loop_int += 1
Next
'SqlConnection_local.Open( )
' Update Database with SqlDataAdapter
SqlDataAdapter_local.Updat e(DataSet_ local.Tabl es("tbl_ad sl_signup" ))
SqlConnection_local.Close( )
Catch ex As Exception
Console.WriteLine(ex.ToStr ing())
End Try
Case 1
Label1.Text = Label1.Text & j.ToString() & " new ADSL TELEPHONY PACKAGE customers; "
Case 2
Label1.Text = Label1.Text & j.ToString() & " new DIALUP customers; "
Case 3
Label1.Text = Label1.Text & j.ToString() & " new DIALUP TELEPHONY PACKAGE customers; "
Case 4
Label1.Text = Label1.Text & j.ToString() & " new TELEPHONY customers; "
Case 5
Label1.Text = Label1.Text & j.ToString() & " new DEALERS; "
'Case 6
'Label6.Text = "You have " & j & " new ADSL customers"
End Select
End If
i += 1
Loop
End If
End Sub
End Class
I'm running an example to use Dataset update function to update DB, but it's not working. therre is no exception, and the dataset's datatable has been uupdated, but not the real SQL DB. I checked the priviledge, it's no problem. Any ideas?
part of the code:
Try
SqlDataAdapter_local.Selec
SqlDataAdapter_local.Selec
SqlDataAdapter_local.Selec
SqlConnection_local.Open()
SqlDataAdapter_local.Fill(
' Create command builder. This line automatically generates the update commands for you, so you don't
' have to provide or create your own.
Dim myDataRowsCommandBuilder As SqlCommandBuilder = New SqlCommandBuilder(SqlDataA
' Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
' key & unique key information to be retrieved unless AddWithKey is specified.
SqlDataAdapter_local.Missi
' SqlDataAdapter_local.Updat
Dim myDataRow, localDataRow As DataRow
Dim loop_int As Integer = 0
For Each myDataRow In DataSet_remote.Tables("tbl
If (loop_int >= local_int(i)) Then
DataSet_local.Tables("tbl_
End If
loop_int += 1
Next
'SqlConnection_local.Open(
' Update Database with SqlDataAdapter
SqlDataAdapter_local.Updat
SqlConnection_local.Close(
Catch ex As Exception
Console.WriteLine(ex.ToStr
End Try
here is the full code.
Imports System.Data.SqlClient
Public Class f_update
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Protected WithEvents Label1 As System.Web.UI.WebControls.
Protected WithEvents get_data As System.Web.UI.WebControls.
Protected WithEvents message As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim SqlDataAdapter_remote As SqlDataAdapter
Dim SqlDataAdapter_local As SqlDataAdapter
Dim SqlConnection_remote As SqlConnection
Dim SqlConnection_local As SqlConnection
Dim DataSet_remote As DataSet
Dim DataSet_local As DataSet
Dim local_int(7) As Integer
Dim remote_int(7) As Integer
Dim outParameter As SqlClient.SqlParameter
Dim i As Integer = 0
Dim spCommand() As String = {"get_adsl_total", "get_apac_total", "get_dial_total", "get_dpac_total", "get_tele_total", "get_dler_total", "get_csid_total"}
Dim sParameter() As String = {"@adsl_intl", "@apac_intl", "@dial_intl", "@dpac_intl", "@tele_intl", "@dler_intl", "@csid_intl"}
Dim tbl_name() As String = {"tbl_adsl_signup", "tbl_adsl_pack", "tbl_dialup_signup", "tbl_dialup_pack", "tbl_telephony_signup", "tbl_dealer_signup", "tbl_customer_id"}
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim adsl_intr, apac_intr, dial_intr, dpac_intr, tele_intr, dler_intr, csid_intr As Integer
'Dim adsl_intl, apac_intl, dial_intl, dpac_intl, tele_intl, dler_intl, csid_intl As Integer
SqlConnection_remote = New SqlConnection("server=(loc
SqlConnection_local = New SqlConnection("server=(loc
SqlDataAdapter_remote = New SqlDataAdapter("select * from customers", SqlConnection_remote)
SqlDataAdapter_local = New SqlDataAdapter("select * from customers", SqlConnection_remote)
DataSet_remote = New DataSet
DataSet_local = New DataSet
'open connection first
If Not Page.IsPostBack Then
SqlConnection_remote.Open(
SqlDataAdapter_remote.Sele
'retrieve updated tbl_customer_id from remote server
SqlDataAdapter_remote.Sele
SqlDataAdapter_remote.Fill
'retrieve tbl_adsl_signup from remote server
SqlDataAdapter_remote.Sele
SqlDataAdapter_remote.Fill
'retrieve tbl_adsl_pack from remote server
SqlDataAdapter_remote.Sele
SqlDataAdapter_remote.Fill
'retrieve tbl_dialup_signup from remote server
SqlDataAdapter_remote.Sele
SqlDataAdapter_remote.Fill
'retrieve tbl_dialup_pack from remote server
SqlDataAdapter_remote.Sele
SqlDataAdapter_remote.Fill
'retrieve tbl_dealer_signup from remote server
SqlDataAdapter_remote.Sele
SqlDataAdapter_remote.Fill
'retrieve tbl_telephony_signup from remote server
SqlDataAdapter_remote.Sele
SqlDataAdapter_remote.Fill
SqlConnection_remote.Close
SqlConnection_local.Open()
'SqlDataAdapter_local.Sele
SqlDataAdapter_local.Selec
'create output parameter to store value return by SP.
Do While i < 7
outParameter = SqlDataAdapter_local.Selec
outParameter.ParameterName
outParameter.Direction = ParameterDirection.Output
outParameter.SqlDbType = SqlDbType.BigInt
SqlDataAdapter_local.Selec
SqlDataAdapter_local.Selec
SqlDataAdapter_local.Selec
SqlDataAdapter_local.Selec
'SqlDataAdapter_local.Sele
SqlDataAdapter_local.Selec
'SqlDataAdapter_local.Fill
If (IsDBNull(SqlDataAdapter_l
local_int(i) = 0
Else
local_int(i) = SqlDataAdapter_local.Selec
End If
i += 1
Loop
SqlConnection_local.Close(
i = 0
'reset i to 0
Do While i < 6 ' not include total users
remote_int(i) = DataSet_remote.Tables(tbl_
'DataSet_remote.Tables(tbl
If remote_int(i) > local_int(i) Then
Dim j As Integer 'j as no. of new users
Dim insert_cmd As Integer
j = remote_int(i) - local_int(i)
Select Case i
Case 0
Label1.Text = "You have " & j.ToString() & " new ADSL customers; "
Try
SqlDataAdapter_local.Selec
SqlDataAdapter_local.Selec
SqlDataAdapter_local.Selec
SqlConnection_local.Open()
SqlDataAdapter_local.Fill(
' Create command builder. This line automatically generates the update commands for you, so you don't
' have to provide or create your own.
Dim myDataRowsCommandBuilder As SqlCommandBuilder = New SqlCommandBuilder(SqlDataA
' Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
' key & unique key information to be retrieved unless AddWithKey is specified.
SqlDataAdapter_local.Missi
' SqlDataAdapter_local.Updat
Dim myDataRow, localDataRow As DataRow
Dim loop_int As Integer = 0
For Each myDataRow In DataSet_remote.Tables("tbl
If (loop_int >= local_int(i)) Then
DataSet_local.Tables("tbl_
End If
loop_int += 1
Next
'SqlConnection_local.Open(
' Update Database with SqlDataAdapter
SqlDataAdapter_local.Updat
SqlConnection_local.Close(
Catch ex As Exception
Console.WriteLine(ex.ToStr
End Try
Case 1
Label1.Text = Label1.Text & j.ToString() & " new ADSL TELEPHONY PACKAGE customers; "
Case 2
Label1.Text = Label1.Text & j.ToString() & " new DIALUP customers; "
Case 3
Label1.Text = Label1.Text & j.ToString() & " new DIALUP TELEPHONY PACKAGE customers; "
Case 4
Label1.Text = Label1.Text & j.ToString() & " new TELEPHONY customers; "
Case 5
Label1.Text = Label1.Text & j.ToString() & " new DEALERS; "
'Case 6
'Label6.Text = "You have " & j & " new ADSL customers"
End Select
End If
i += 1
Loop
End If
End Sub
End Class
ASKER
Hi nishikanth,
I'm just trying to update the table with dataset, the problem is not the if condition, I know it's only running once, the problem is the update is not working.
I am trying to update the tables from remote DB to a local DB. I could not use the SQL server replication because of lacking enough priviledge. So I need to do it automatically, not use sql to do it manunally.
I'm just trying to update the table with dataset, the problem is not the if condition, I know it's only running once, the problem is the update is not working.
I am trying to update the tables from remote DB to a local DB. I could not use the SQL server replication because of lacking enough priviledge. So I need to do it automatically, not use sql to do it manunally.
ASKER
I try to compare the number of records on both tables which have the same schema, but on different servers, if the number of records in remote server are greater than those of local server, then insert the new records into the local server. That's what I try to do.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi jnhorst ,
thank you for your post, I tried your way, it's working. But I have to provide so many parameters, that's what I don't like. It's not generated automatically. Because I use dynamic query.
And I found the problem is the following method, after I changed to addrow() method, and it's working. But I don't know why ImportRow() method failed. From the debug, I could see the recoreds have been added to the Dataset, but not updating to the DB.
DataSet_local.Tables("tbl_ adsl_signu p").Import Row(myData Row)
I used the following line, so it's generating all methods automatically. That's why there is no insert method in my code.
' Create command builder. This line automatically generates the update commands for you, so you don't have to provide or create your own.
Dim myDataRowsCommandBuilder As SqlCommandBuilder = New SqlCommandBuilder(SqlDataA dapter_loc al)
thank you for your post, I tried your way, it's working. But I have to provide so many parameters, that's what I don't like. It's not generated automatically. Because I use dynamic query.
And I found the problem is the following method, after I changed to addrow() method, and it's working. But I don't know why ImportRow() method failed. From the debug, I could see the recoreds have been added to the Dataset, but not updating to the DB.
DataSet_local.Tables("tbl_
I used the following line, so it's generating all methods automatically. That's why there is no insert method in my code.
' Create command builder. This line automatically generates the update commands for you, so you don't have to provide or create your own.
Dim myDataRowsCommandBuilder As SqlCommandBuilder = New SqlCommandBuilder(SqlDataA
the code will not be called evertime the page is submitted, since the code for update is in the if condition Not iSPOSTBACK. Only the first time when the page is loaded, the code will be executed..
also for some info abt update....
check this
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconupdatingdatabasewithdataadapterdataset.asp