列不属于表Table /找不到表0

时间:2013-08-30 08:33:16

标签: asp.net vb.net

我的小型网络应用程序出现问题。当两个用户添加记录并同时更新时。

错误出现“列不属于表有时无法找到表0.”我不知道从哪里开始。请帮我。我创建了一个库,就在这里。

我的ApplicationTool类。

Private oSqlConnection As SqlConnection
Private oSqlDataAdapter As SqlDataAdapter
Private oSqlCommand As SqlCommand
Private oSqlTransaction As SqlTransaction
Public Dr As DataRow
Public Ds As DataSet

Private _strCommand As String

Public Property strCommand() As String
    Get
        Return _strCommand
    End Get

    Set(ByVal value As String)
        If Not InTransaction Then RollBack_Transaction()
        _strCommand = "SET DATEFORMAT mdy " & vbCrLf & value
        DbQuery()
    End Set
End Property

Protected Sub DbQuery()

    If Not InTransaction Then
        RollBack_Transaction()

        oSqlCommand = New SqlCommand(_strCommand, oSqlConnection)
    Else
        oSqlCommand = oSqlConnection.CreateCommand
        If _InTransaction_Initial Then
            oSqlConnection.Open()
            oSqlTransaction = oSqlConnection.BeginTransaction(IsolationLevel.ReadCommitted)
            _InTransaction_Initial = False
        End If

        oSqlCommand.Transaction = oSqlTransaction
        oSqlCommand.CommandText = _strCommand
        oSqlCommand.CommandTimeout = 0
    End If

    oSqlDataAdapter = New SqlDataAdapter(oSqlCommand)
    Ds = New DataSet
    oSqlDataAdapter.Fill(Ds)

End Sub

Private _InTransaction_Initial As Boolean = False
Private InTransaction As Boolean

Public Sub StartTransaction()
    _InTransaction_Initial = True
    InTransaction = True
End Sub
Public Sub RollBack_Transaction()
    Try
        oSqlTransaction.Rollback()
    Catch ex As Exception
    End Try
    Try
        oSqlConnection.Close()
    Catch ex As Exception
    End Try

    InTransaction = False
End Sub

Public Sub Commit_Transaction()
    Try
        oSqlTransaction.Commit()
    Catch ex As Exception
    End Try
    Try
        oSqlConnection.Close()
    Catch ex As Exception
    End Try

    InTransaction = False
End Sub

Function dsCount() As Long
    Return Ds.Tables(0).Rows.Count
End Function

Public Sub New()
    oSqlConnection = New SqlConnection("initial catalog=" & MainDb & "; data source=" & ServerName & "; user id=" & SqlID & "; password=" & SqlPassword & "; Application Name=APPTech-MIMS-Web; Connection Timeout=30")
    Dim oString As String = oSqlConnection.ConnectionString
    FileDbName = GetConfigKey("FileDb")
End Sub

我的文档课程。

Public Class oDocuments
Inherits ApplicationTool


Public AppId As Long = -1 
Public AppType As oAppTypes = Nothing
Public DocNum As String

Public DocDate As DateTime
Public RefNum As String
Public PriceList As Long
Public CardCode As String
Public CardName As String
Public WhsCode As String
Public ToWhsCode As String
Public BinCode As String
Public ToBinCode As String

Public DocStatus As oDocStatuses = oDocStatuses.New_Record

Public SubmittedDate As DateTime
Public SubmittedBy As Long
Public CreatedBy As Long = GetUSER_ID

Public Lines As New oDocuments_Lines
Public Distribution As New oDocuments_Distribution

Private oDistribution As Boolean = False


Sub New(Optional ByVal oAppType As oAppTypes = Nothing, Optional ByVal oSerial As Boolean = False)
    If Not IsNothing(oAppType) Then AppType = oAppType
    Lines.Apptype = AppType
    Distribution.AppType = AppType

    If oSerial Then
        oDistribution = True
    End If
End Sub

Function ValidateData() As Boolean
    Try

        If IsNothing(AppType) Or AppType = 0 Then SetMessage("Invalid application type.", oMessageTypes.oError) : Return False

        Return True
    Catch ex As Exception
        SetMessage(ex)
    End Try
    Return False
End Function

Private oDs As DataSet
Public ReadOnly Property Count() As Integer
    Get
        Return oDs.Tables(0).Rows.Count
    End Get
End Property

Function GetRecordSet() As Data.DataTable
    Return Ds.Tables(0)
End Function

Private Function SetVariables() As Boolean
    oDs = Ds.Copy
    Dim hasData As Boolean = False

    If dsCount() > 0 Then
        AppId = GetDs("AppId") 
        AppType = GetDs("AppType")

        DocNum = GetDs("DocNum")
        DocDate = GetDs("DocDate")
        PriceList = GetDs("PriceList")
        WhsCode = GetDs("WhsCode")
        ToWhsCode = GetDs("ToWhsCode")
        BinCode = GetDs("BinCode")
        ToBinCode = GetDs("ToBinCode")
        RefNum = GetDs("RefNum")
        CardCode = GetDs("CardCode")
        CardName = GetDs("CardName")

        DocStatus = GetDs("DocStatus")

        SubmittedDate = IIf(IsDBNull(GetDs("SubmittedDate")), Now, GetDs("SubmittedDate"))
        SubmittedBy = GetDs("SubmittedBy")
        CreatedBy = GetDs("CreatedBy")

        hasData = True
    End If

    Return hasData
End Function


Function GetByAppId(ByVal oAppId As Long) As Boolean
    Try
        strCommand = "SELECT Top 1 * FROM " & GetTableName(AppType, oTableLevel.Header) & "(nolock) WHERE AppId = '" & oAppId & "'"

        Return SetVariables()
    Catch ex As Exception
        SetMessage(ex)
    End Try
    Return False
End Function

Function Add() As Boolean
    Try
        If Not ValidateData() Then Return False

        GetNextNumber(AppType)
        AppId = GetNewAppId

        If Not Update Then
            Return False

        End If

        Return True
    Catch ex As Exception
        SetMessage(ex)
    End Try
End Function

Function Update() As Boolean
    Try
        If Not ValidateData() Then Return False

        strCommand = "UPDATE " & GetTableName(AppType, oTableLevel.Header) & " SET " & _
                    "DocNum = '" & TrimData(DocNum) & "', " & _
                    "DocDate = '" & TrimData(DocDate) & "', " & _
                    "RefNum = '" & TrimData(RefNum) & "', " & _
                    "PriceList = '" & TrimData(PriceList) & "', " & _
                    "WhsCode = '" & TrimData(WhsCode) & "', " & _
                    "ToWhsCode = '" & TrimData(ToWhsCode) & "', " & _
                    "BinCode = '" & TrimData(BinCode) & "', " & _
                    "ToBinCode = '" & TrimData(ToBinCode) & "', " & _
                    "CardCode = '" & TrimData(CardCode) & "', " & _
                    "CardName = '" & TrimData(CardName) & "', " & _
                    "ModifiedDate = GETDATE(), " & _
                    "ModifiedBy = '" & GetUSER_ID & "' " & _
                    "WHERE AppId = '" & AppId & "' "

        Return True
    Catch ex As Exception
        SetMessage(ex)
    End Try
End Function

Function Submit() As Boolean
    Try
        If Not ValidateAppId(AppId) Then Return False

        Return ChangeDocStatus(AppType, AppId, oDocStatuses.Submitted)
    Catch ex As Exception
        SetMessage(ex)
    End Try
    Return False
End Function

结束班

在他们添加或更新数据时的代码中。

Private Function SaveDocument()As Boolean         Dim oDocument As New WEB_Library.oDocuments(AppType.Text)

    With oDocument
        .AppId = AppId.Text
        .DocNum = AppId.Text
        .RefNum = RefNum.Text
        .PriceList = -1
        .WhsCode = WhsCode.Text

        .BinCode = BinCode.Text
        .CardCode = CardCode.Text
        .CardName = CardName.Text
        .ToWhsCode = ToWhsCode.Text

        If AppId.Text = "-1" Then
            If .Add Then
                AppId.Text = .GetNewAppId
                DocNum.Text = .GetNewAppId

                Return True
            End If

        Else
            If .Update Then
                Return True

            End If

        End If
    End With
End Function

此代码用于调用记录。

Dim oDocument As New WEB_Library.oDocuments(AppType.Text)

        With oDocument
            If .GetByAppId(DocNum.Text) Then
                AppId.Text = .AppId
                DocNum.Text = .AppId
                DocDate.Text = .DocDate
                WhsCode.Text = .WhsCode
                BinCode.Text = .BinCode
                CardCode.Text = .CardCode
                CardName.Text = .CardName
                ToWhsCode.Text = .ToWhsCode
                DocStatus.Text = .DocStatus
                PriceList.Text = .PriceList
                ToWhsCode.Text = .ToWhsCode
                ToBinLoc.Text = .ToBinCode
                RefNum.Text = .RefNum
            Else
                DocDate.Text = ""
                WhsCode.Text = "-1"
                BinCode.Text = "-1"
                CardCode.Text = "-1"
                CardName.Text = "-1"
                ToWhsCode.Text = "-1"
                DocStatus.Text = "-1"
                PriceList.Text = "-1"
                ToWhsCode.Text = "-1"
                ToBinLoc.Text = "-1"
                RefNum.Text = ""
            End If

        End With

        Select Case DocStatus.Text = "-1"
            Case -1
                cmdSubmit.Visible = True

            Case 2
                cmdSubmit.Visible = False

        End Select

        Call SetGrid()

然后在Grid中绑定。

Sub SetGrid()
    Try
        Dim oDocument As New WEB_Library.oDocuments(AppType.Text)
        gMain.DataSource = Nothing
        gMain.DataBind()


        If oDocument.GetByAppId(AppId.Text) Then

            gMain.DataSource = oDocument.Lines.GetRecordSet.DefaultView
            gMain.DataBind()

            Dim cmbItemCode As New DropDownList
            cmbGetItemCode(cmbItemCode)

            Dim dgObj As DataGrid = gMain

            For Each oGridItem In dgObj.Items
                Dim ItemCode As DropDownList = oGridItem.FindControl("ItemCode")
                If oDocument.Lines.GetByLineId(dgObj.DataKeys(oGridItem.ItemIndex)) Then
                    Dim InvQty As AppTextBox = oGridItem.FindControl("InvQty")

                    WebCtrl.cmbCopyDataBind(cmbItemCode, ItemCode)
                    WebCtrl.cmbFocusValue(ItemCode, oDocument.Lines.ItemCode)

                End If
            Next
        End If
    Catch ex As Exception
        WebCtrl.SetMessage(ex, WEB_Library.ApplicationTool.oMessageTypes.oError)
    End Try
End Sub

希望听到积极的回应。我在所有的WEB应用程序中都在努力解决这个问题。 :(

谢谢。

1 个答案:

答案 0 :(得分:1)

New DataSet分配给数据集变量

时可能会发生这种情况

如果你只在数据集中使用1个表,那么如果你使用没有数据集的数据表会更容易。

Public tbl as New DataTable

oSqlDataAdapter.Fill(tbl)

这不会让你犯错误......