Problem With Get More Table Instead of one Table
-
Hi all, I have one web methode in my web service,that can only get one table from my application and it is without Transaction,
<WebMethod()> _
Public Function OprationONTbl(ByRef ds As Data.DataSet, ByVal TblName As String, ByVal Insert As Boolean, ByVal Update As Boolean, ByVal StrWhere As String, ByVal retValue As Boolean, ByRef StrRetValue As String) As String
Dim cn As New SqlConnection
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim cBuilder As New SqlCommandBuilder(da)Try
'-*-> cn
Dim cls As New clsGeneral
cn.ConnectionString = cls.GetConnection
cls = Nothing'-*-> cmd
cmd.CommandText = "Select * From " & TblName
If StrWhere.Trim <> "" Then cmd.CommandText = cmd.CommandText & " WHERE " & StrWhere
cmd.Connection = cn'-*-> da
da.SelectCommand = cmd'**************************************'
If Not Insert And Not Update Then
da.Fill(ds, TblName)
End If
'**************************************'
If Insert Then
da.InsertCommand = cBuilder.GetInsertCommand
da.Update(ds.Tables(TblName))
If retValue Then
cmd.CommandText = "Select Max(Id) from " & TblName
cn.Open()
StrRetValue = cmd.ExecuteScalar
cn.Close()
End If
End If
'**************************************'
If Update Then
Dim value() As String = StrWhere.Split("*")
cmd.CommandText = "UpDate " & TblName & " SET " & value(1) & " WHERE " & value(0)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End IfReturn ""
Catch ex As Exception
Return "OprationONTbl : " & ex.MessageFinally
If cn.State = ConnectionState.Open Then cn.Close()
cn = Nothing
cmd = Nothing
da = Nothing
cBuilder = Nothing
End TryEnd Function
Now i want get more table form my application and with transaction, that if i send to webService two tables, with one transaction tow tables be full of data,
now i changed above web method but my result is not true ,i test to many state of this code but it has different Error for Inserting data,I don't know how i can change this web method for doing my work true.
i write this but it is mistake,
Dim myTblName() As String = TblName.Split("$")
'-*-> cmd
For i As Integer = 0 To myTblName.Length - 1
cmd.CommandText = "Select * From " & myTblName(i)
If StrWhere.Trim <> "" Then cmd.CommandText = cmd.CommandText & " WHERE " & StrWhere
cmd.Connection = cn
'-*-> da
da.SelectCommand = cmd
'**************************************'
If Not Insert And Not Update Then
da.Fill(ds, myTbl -
Hi all, I have one web methode in my web service,that can only get one table from my application and it is without Transaction,
<WebMethod()> _
Public Function OprationONTbl(ByRef ds As Data.DataSet, ByVal TblName As String, ByVal Insert As Boolean, ByVal Update As Boolean, ByVal StrWhere As String, ByVal retValue As Boolean, ByRef StrRetValue As String) As String
Dim cn As New SqlConnection
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim cBuilder As New SqlCommandBuilder(da)Try
'-*-> cn
Dim cls As New clsGeneral
cn.ConnectionString = cls.GetConnection
cls = Nothing'-*-> cmd
cmd.CommandText = "Select * From " & TblName
If StrWhere.Trim <> "" Then cmd.CommandText = cmd.CommandText & " WHERE " & StrWhere
cmd.Connection = cn'-*-> da
da.SelectCommand = cmd'**************************************'
If Not Insert And Not Update Then
da.Fill(ds, TblName)
End If
'**************************************'
If Insert Then
da.InsertCommand = cBuilder.GetInsertCommand
da.Update(ds.Tables(TblName))
If retValue Then
cmd.CommandText = "Select Max(Id) from " & TblName
cn.Open()
StrRetValue = cmd.ExecuteScalar
cn.Close()
End If
End If
'**************************************'
If Update Then
Dim value() As String = StrWhere.Split("*")
cmd.CommandText = "UpDate " & TblName & " SET " & value(1) & " WHERE " & value(0)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End IfReturn ""
Catch ex As Exception
Return "OprationONTbl : " & ex.MessageFinally
If cn.State = ConnectionState.Open Then cn.Close()
cn = Nothing
cmd = Nothing
da = Nothing
cBuilder = Nothing
End TryEnd Function
Now i want get more table form my application and with transaction, that if i send to webService two tables, with one transaction tow tables be full of data,
now i changed above web method but my result is not true ,i test to many state of this code but it has different Error for Inserting data,I don't know how i can change this web method for doing my work true.
i write this but it is mistake,
Dim myTblName() As String = TblName.Split("$")
'-*-> cmd
For i As Integer = 0 To myTblName.Length - 1
cmd.CommandText = "Select * From " & myTblName(i)
If StrWhere.Trim <> "" Then cmd.CommandText = cmd.CommandText & " WHERE " & StrWhere
cmd.Connection = cn
'-*-> da
da.SelectCommand = cmd
'**************************************'
If Not Insert And Not Update Then
da.Fill(ds, myTblDon't use Max(Id) and a transaction to get the id, just call "select scope_identity()" after you have made the insert. That is the normal method to get the id, and it will use less resources on the server.
Despite everything, the person most likely to be fooling you next is yourself.