W
Wael
Hi,
I have a data adapter and trying to connect it to multiple tables. I am
not sure if it is possible and if so, I can't find what I am doing
wrong. I am using HTML controls because the original page was created
with another software, but I changed all the controls to "Run as Server
Control". The loading works fine, but the updates don't. How can I
attach two SQL statements to one data adapter? Or do I have to use two
data adapters?
Option Explicit On
Option Strict On
Imports System.Data.SqlClient
Public Class _62031
Inherits System.Web.UI.Page
' Variables to hold local copies of the global data objects
Dim lv_PageDataAdapter As SqlDataAdapter
Dim lv_PageConnection As SqlConnection
Dim lv_PageDataSet As DataSet
Dim lv_TableName1 As String = "INCLUS"
Dim lv_TableName2 As String = "EXCLUS"
Dim lv_id1 As Integer
Dim lv_id2 As Integer
Dim Radio(1) As HtmlControls.HtmlInputRadioButton ' A holder for
radio buttons used to get indexes
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Make local copies of the data objects to make it easier to
manipulate
lv_id1 = CType(Session.Contents("gv_id1"), Integer)
lv_id2 = CType(Session.Contents("gv_id2"), Integer)
lv_PageConnection = CType(Session.Contents("gv_Connection"),
SqlConnection)
lv_PageDataAdapter = CType(Session.Contents("gv_DataAdapter"),
SqlDataAdapter)
lv_PageDataSet = CType(Session.Contents("gv_DataSet"), DataSet)
If Not Page.IsPostBack Then
' Make sure we have an open connection
If lv_PageConnection.State <> ConnectionState.Open Then
lv_PageConnection.Open()
lv_PageDataAdapter.SelectCommand = _
New SqlCommand("SELECT id1, id2, i1 FROM " & _
lv_TableName1 & " WHERE id1=" & lv_id1 & " AND
id2=" & lv_id2, _
lv_PageConnection)
lv_PageDataAdapter.Fill(lv_PageDataSet.Tables.Add(lv_TableName1))
ID1.Value =
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("id1").ToString
ID2.Value =
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("id2").ToString
i1.Value =
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("i1").ToString)
' Exclusions
lv_TableName2 = "EXCLUS"
lv_PageDataAdapter.SelectCommand = _
New SqlCommand("SELECT id1, id2, e1 FROM " & _
lv_TableName2 & " WHERE id1=" & lv_id1 & " AND
id2=" & lv_id2, _
lv_PageConnection)
lv_PageDataAdapter.Fill(lv_PageDataSet.Tables.Add(lv_TableName2))
e1.Value =
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("e1").ToString)
End If
End Sub
Private Sub Submit1_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Submit1.ServerClick
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("i1") = i1
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("e1") = e1
Try
sbUpdateData(lv_PageDataSet)
lv_PageDataSet.AcceptChanges()
Catch ex As SqlException
End Try
End Sub
Private Sub sbUpdateData(ByVal pardv As DataSet)
Dim lv_CommandBuilder As New
SqlCommandBuilder(lv_PageDataAdapter)
If lv_PageConnection.State <> ConnectionState.Open Then
lv_PageConnection.Open()
lv_PageDataAdapter.UpdateCommand =
lv_CommandBuilder.GetUpdateCommand
lv_PageDataAdapter.Update(pardv.Tables(lv_TableName1))
End Sub
End Class
I have a data adapter and trying to connect it to multiple tables. I am
not sure if it is possible and if so, I can't find what I am doing
wrong. I am using HTML controls because the original page was created
with another software, but I changed all the controls to "Run as Server
Control". The loading works fine, but the updates don't. How can I
attach two SQL statements to one data adapter? Or do I have to use two
data adapters?
Option Explicit On
Option Strict On
Imports System.Data.SqlClient
Public Class _62031
Inherits System.Web.UI.Page
' Variables to hold local copies of the global data objects
Dim lv_PageDataAdapter As SqlDataAdapter
Dim lv_PageConnection As SqlConnection
Dim lv_PageDataSet As DataSet
Dim lv_TableName1 As String = "INCLUS"
Dim lv_TableName2 As String = "EXCLUS"
Dim lv_id1 As Integer
Dim lv_id2 As Integer
Dim Radio(1) As HtmlControls.HtmlInputRadioButton ' A holder for
radio buttons used to get indexes
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Make local copies of the data objects to make it easier to
manipulate
lv_id1 = CType(Session.Contents("gv_id1"), Integer)
lv_id2 = CType(Session.Contents("gv_id2"), Integer)
lv_PageConnection = CType(Session.Contents("gv_Connection"),
SqlConnection)
lv_PageDataAdapter = CType(Session.Contents("gv_DataAdapter"),
SqlDataAdapter)
lv_PageDataSet = CType(Session.Contents("gv_DataSet"), DataSet)
If Not Page.IsPostBack Then
' Make sure we have an open connection
If lv_PageConnection.State <> ConnectionState.Open Then
lv_PageConnection.Open()
lv_PageDataAdapter.SelectCommand = _
New SqlCommand("SELECT id1, id2, i1 FROM " & _
lv_TableName1 & " WHERE id1=" & lv_id1 & " AND
id2=" & lv_id2, _
lv_PageConnection)
lv_PageDataAdapter.Fill(lv_PageDataSet.Tables.Add(lv_TableName1))
ID1.Value =
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("id1").ToString
ID2.Value =
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("id2").ToString
i1.Value =
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("i1").ToString)
' Exclusions
lv_TableName2 = "EXCLUS"
lv_PageDataAdapter.SelectCommand = _
New SqlCommand("SELECT id1, id2, e1 FROM " & _
lv_TableName2 & " WHERE id1=" & lv_id1 & " AND
id2=" & lv_id2, _
lv_PageConnection)
lv_PageDataAdapter.Fill(lv_PageDataSet.Tables.Add(lv_TableName2))
e1.Value =
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("e1").ToString)
End If
End Sub
Private Sub Submit1_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Submit1.ServerClick
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("i1") = i1
lv_PageDataSet.Tables(lv_TableName1).Rows(0).Item("e1") = e1
Try
sbUpdateData(lv_PageDataSet)
lv_PageDataSet.AcceptChanges()
Catch ex As SqlException
End Try
End Sub
Private Sub sbUpdateData(ByVal pardv As DataSet)
Dim lv_CommandBuilder As New
SqlCommandBuilder(lv_PageDataAdapter)
If lv_PageConnection.State <> ConnectionState.Open Then
lv_PageConnection.Open()
lv_PageDataAdapter.UpdateCommand =
lv_CommandBuilder.GetUpdateCommand
lv_PageDataAdapter.Update(pardv.Tables(lv_TableName1))
End Sub
End Class