我现在收到此错误“'iTrac.SalesOrder'的类型初始值设定项引发了异常。”

时间:2019-07-25 20:07:22

标签: vb.net

我有一个可以正常工作的功能iTrac.SalesOrder.SalesOrdersByBody_JB,当我尝试从Web服务调用它时,它失败并引发异常

  

“'iTrac.SalesOrder'的类型初始值设定项引发了异常。”

我看到这是一个相当普遍的情况,我确实经历了所有类似的问题,但是我没有看到问题的任何答案,或者也许我是一个新手,无法识别答案...?< / p>

经历了所有类似的问题。

Private Sub BrowserMailSender(obj As Object, e As EventArgs)
    Dim x As New List(Of iTrac.JBSODetail)
    Try
        FileIO.WriteToFile("service is started:" + Now + vbNewLine)
        x = iTrac.SalesOrder.SalesOrdersByBody_JB(241)
        FileIO.WriteToFile(x.ToString)
    Catch ex As Exception
        FileIO.WriteToFile(ex.Message + vbNewLine)
    End Try

End Sub

引发异常的功能

Public Shared Function SalesOrdersByBody_JB(ByVal BodyID As String) As List(Of JBSODetail)

    Dim SalesOrderList As New List(Of JBSODetail)
    Using context = New iTracContext
        Dim BOMList = New List(Of Integer)
        Dim StatusList As New List(Of String)
        BOMList = context.BillOfMaterials.Where(Function(b) b.Child = 
                  BodyID And b.ParentClass = "RTS").Select(Function(b) 
                                                    b.Parent).ToList

        If BOMList Is Nothing Then
            Return Nothing
        Else
            StatusList.Add("OPEN")
            StatusList.Add("HOLD")
            StatusList.Add("BACKORDER")
        End If
        Dim query =context.JBSODetails.Include
                   ("Part").Include("SOHeader").AsQueryable
        If Not String.IsNullOrEmpty(BodyID) Then
            query = query.Where(Function(s) 
                    BOMList.Contains(s.pid) And 
                    StatusList.Contains(s.status.ToUpper))
        End If

        Return query.OrderBy(Function(s) s.promised_date).ToList

    End Using

End Function
Imports System.Data.SqlClient
Imports System.ComponentModel.DataAnnotations
Imports iTrac

<Table("tblSODetails")>
Public Class SalesOrder

    <Key()>
    Public Property sid As Decimal
    Public Property SalesOrderNumber As String
    Public Property SalesOrderLine As String
    Public Property NeedDate As Date
    Public Property PromisedDate As Date
    Public Property ShipTo As String
    Public Property Customer As String
    <Column("UnitPriceOfOrder")>
    Public Property UnitPrice As Decimal?
    <Column("PriceUnitOfOrder")>
    Public Property PriceUnit As String
    Public Property OrderQuantity As Decimal?
    Public Property OrderBalance As Decimal?
    Public Property OrderEntryDate As Date
    <Column("Material")>
    Public Property CustomerPartNumber As String
    Public Property LastUpdated As Date
    Public Property CustomerPO As String
    <NotMapped()>
    Public Property NormalizedPartNumber As String
    Public Property OfficeNote As String

    Public Property pid As Integer?
    Public Property aid As Integer?
    Public Property cid As Integer?
    Public Property TransferTime As Date
    Public Property ShipVia As String

    <NotMapped>
    Public Property RunningTotal As Integer?

    <ForeignKey("pid")>
    Public Property Inventory As CompiledInventory

    <ForeignKey("pid")>
    Public Property Part As Part

    <NotMapped()>
    Public Property RTS
    <NotMapped()>
    Public Property Plate
    <NotMapped()>
    Public Property Machined

    <NotMapped()>
    Public Property Status

    Private Shared ConnectionString As String = BrowserUtilities.Settings.ConnectionString

    Public ReadOnly Property Blocked As Boolean
        Get
            Return ShippingBlock.BlockExists(SalesOrderNumber, SalesOrderLine)
        End Get
    End Property

    Public Shared Function OpenSalesOrderList(ByVal CustomerName As String,
                                                ByVal ShipToAddress As String,
                                                ByVal CustomerPartNumber As String,
                                                ByVal CutOffDate As String,
                                                ByVal NormalizedID As String,
                                                ByVal UpdatedWindow As String,
                                                Optional ByVal CustomerPO As String = "") As List(Of SalesOrder)

        Dim SalesOrderList As New List(Of SalesOrder)

        Using Connection As New SqlConnection(ConnectionString)

            Connection.Open()

            Dim sqlString = ""
            Dim sqlWhereClause = ""
            If CustomerName <> "" Then
                sqlWhereClause = " where customer = '" & CustomerName & "'"
            End If

            If ShipToAddress <> "" Then
                If sqlWhereClause > "" Then
                    sqlWhereClause += " and shipto = '"
                Else
                    sqlWhereClause += " where shipto = '"
                End If
                sqlWhereClause += ShipToAddress + "'"
            End If

            If CustomerPartNumber <> "" Then
                If sqlWhereClause > "" Then
                    sqlWhereClause += " and material like '"
                Else
                    sqlWhereClause += " where material like '"
                End If
                sqlWhereClause += CustomerPartNumber + "'"
            End If

            If CustomerPO <> "" Then
                If sqlWhereClause > "" Then
                    sqlWhereClause += " and customerpo = '"
                Else
                    sqlWhereClause += " where customerpo = '"
                End If
                sqlWhereClause += CustomerPO + "'"
            End If

            If NormalizedID <> "" Then
                If NormalizedID <> "0" Then
                    If sqlWhereClause > "" Then
                        sqlWhereClause += " and pid = '"
                    Else
                        sqlWhereClause += " where pid = '"
                    End If
                    sqlWhereClause += NormalizedID + "'"
                End If
            End If

            If CutOffDate <> "" And CustomerPO = "" Then
                If sqlWhereClause > "" Then
                    sqlWhereClause += " and promiseddate <= '"
                Else
                    sqlWhereClause += " where promiseddate <= '"
                End If
                sqlWhereClause += CutOffDate + "'"
            End If

            If UpdatedWindow <> "" Then
                If sqlWhereClause > "" Then
                    sqlWhereClause += " and "
                Else
                    sqlWhereClause += "where "
                End If
                sqlWhereClause += "lastupdated between '" & Now.AddHours(-Val(UpdatedWindow)) & "' and '" & Now & "'"
            End If

            sqlString = "Select s.*, (select NormalizedPartNumber from tblPart where id = pid) as NormalizedPartNumber from tblSODetails as s" & sqlWhereClause
            If UpdatedWindow <> "" Then
                sqlString += " order by customer, normalizedpartnumber, promiseddate"
            Else
                sqlString += " order by promiseddate"
            End If

            Dim command = New SqlCommand(sqlString, Connection)

            Dim dataReader As SqlDataReader = command.ExecuteReader()

            While dataReader.Read()
                Dim so = New SalesOrder()
                so.MapDataReader(dataReader, 1)
                SalesOrderList.Add(so)
            End While

        End Using

        Return SalesOrderList

    End Function

    Private Sub MapDataReader(ByVal dataReader As IDataReader, ByVal Method As Integer)

        Select Case Method
            Case 0, 1
                sid = If(IsDBNull(dataReader("sid")), "", dataReader("sid"))
                SalesOrderNumber = If(IsDBNull(dataReader("salesordernumber")), "", dataReader("salesordernumber"))
                SalesOrderLine = If(IsDBNull(dataReader("salesorderline")), "", dataReader("salesorderline"))
                PromisedDate = If(IsDBNull(dataReader("promiseddate")), "", dataReader("promiseddate"))
                ShipTo = If(IsDBNull(dataReader("shipto")), "", dataReader("shipto"))
                Customer = If(IsDBNull(dataReader("customer")), "", dataReader("customer"))
                UnitPrice = If(IsDBNull(dataReader("unitpriceoforder")), 0, dataReader("unitpriceoforder"))
                PriceUnit = If(IsDBNull(dataReader("priceunitoforder")), "", dataReader("priceunitoforder"))
                OrderQuantity = If(IsDBNull(dataReader("orderquantity")), "", dataReader("orderquantity"))
                OrderBalance = If(IsDBNull(dataReader("orderbalance")), "", dataReader("orderbalance"))
                OrderEntryDate = If(IsDBNull(dataReader("orderentrydate")), "", dataReader("orderentrydate"))
                CustomerPartNumber = If(IsDBNull(dataReader("material")), "", dataReader("material"))
                LastUpdated = If(IsDBNull(dataReader("lastupdated")), "", dataReader("lastupdated"))
                CustomerPO = If(IsDBNull(dataReader("customerpo")), "", dataReader("customerpo"))
                OfficeNote = If(IsDBNull(dataReader("officenote")), "", dataReader("officenote"))
                pid = If(IsDBNull(dataReader("pid")), 0, dataReader("pid"))
                aid = If(IsDBNull(dataReader("aid")), 0, dataReader("aid"))
                cid = If(IsDBNull(dataReader("cid")), 0, dataReader("cid"))
                TransferTime = dataReader("transfertime")
                ShipVia = If(IsDBNull(dataReader("shipvia")), "", dataReader("shipvia"))
                NeedDate = If(IsDBNull(dataReader("new_promised_date")), dataReader("promiseddate"), dataReader("new_promised_date"))
                If Method = 1 Then NormalizedPartNumber = If(IsDBNull(dataReader("normalizedpartnumber")), "", dataReader("normalizedpartnumber"))
            Case 2
                Customer = If(IsDBNull(dataReader("customer")), "Unknown", dataReader("customer"))
        End Select

    End Sub

    Public Shared Function SalesOrdersByBody(ByVal BodyID As String, ByVal CustomerPartNumber As String) As List(Of SalesOrder)

        Dim SalesOrderList As New List(Of SalesOrder)
        Dim CP As New Part
        Using Connection As New SqlConnection(ConnectionString)

            Dim sqlString = ""
            Connection.Open()

            If BodyID > "" Then
                sqlString = "Select s.*, d.new_promised_date from tblPart as p " &
                "inner join tblSODetails as s on p.id = s.pid " &
                "inner join tblJBSODetails d on d.sales_order = s.salesordernumber " &
                "inner join tblBillOfMaterial as b on p.id = b.parent and parentclass = 'rts' " &
                "where b.child = " & BodyID & " order by s.promiseddate"
            Else
                sqlString = "Select s.*, d.new_promised_date from tblPart as p " &
                "inner join tblSODetails as s on s.pid = p.id " &
                "inner join tblJBSODetails d on d.sales_order = s.salesordernumber " &
                "where p.CustomerPartNumber = '" & CustomerPartNumber & "' " &
                "order by s.promiseddate"
            End If

            Dim command = New SqlCommand(sqlString, Connection)

            Dim dataReader As SqlDataReader = command.ExecuteReader()

            While dataReader.Read()
                Dim so = New SalesOrder()
                so.MapDataReader(dataReader, 0)
                SalesOrderList.Add(so)
            End While

        End Using

        Return SalesOrderList

    End Function
    Public Shared Function SalesOrdersByBody_JB(ByVal BodyID As String) As List(Of JBSODetail)

        Dim SalesOrderList As New List(Of JBSODetail)
        Using context = New iTracContext
            Dim BOMList = New List(Of Integer)
            Dim StatusList As New List(Of String)
            BOMList = context.BillOfMaterials.Where(Function(b) b.Child = BodyID And b.ParentClass = "RTS").Select(Function(b) b.Parent).ToList

            If BOMList Is Nothing Then
                Return Nothing
            Else
                StatusList.Add("OPEN")
                StatusList.Add("HOLD")
                StatusList.Add("BACKORDER")
            End If
            Dim query = context.JBSODetails.Include("Part").Include("SOHeader").AsQueryable
            If Not String.IsNullOrEmpty(BodyID) Then
                query = query.Where(Function(s) BOMList.Contains(s.pid) And StatusList.Contains(s.status.ToUpper))
            End If
            Return query.OrderBy(Function(s) s.promised_date).ToList

        End Using

    End Function


    Public Shared Function SalesOrdersByPartID(ByVal PartID As Integer) As List(Of SalesOrder)

        Dim SalesOrderList As New List(Of SalesOrder)

        Using Connection As New SqlConnection(ConnectionString)

            Dim sqlString = ""
            Connection.Open()

            sqlString = "Select s.*, d.new_promised_date from tblPart as p " &
            "inner join tblSODetails as s on p.id = s.pid " &
            "inner join tblJBSODetails d on d.sales_order = s.salesordernumber " &
            "inner join tblBillOfMaterial as b on p.id = b.parent and parentclass = 'rts' " &
            "where b.parent = " & PartID & " order by s.promiseddate"


            Dim command = New SqlCommand(sqlString, Connection)

            Dim dataReader As SqlDataReader = command.ExecuteReader()

            While dataReader.Read()
                Dim so = New SalesOrder()
                so.MapDataReader(dataReader, 0)
                SalesOrderList.Add(so)
            End While

        End Using

        Return SalesOrderList
        'Using context = New iTracContext
        '    Return context.SalesOrders.Where(Function(s) s.pid = PartID).ToList
        'End Using
    End Function
    Public Shared Function SalesOrdersByPartID_JB(ByVal PartID As Integer) As List(Of JBSODetail)
        Dim SalesOrderList As New List(Of JBSODetail)
        Using context = New iTracContext
            Dim query = context.JBSODetails.Include("SOHeader").Include("Part").AsQueryable
            If Not String.IsNullOrEmpty(PartID) Then query = query.Where(Function(s) s.pid = PartID)
            query = query.Where(Function(s) s.status = "Open")
            Return query.OrderBy(Function(s) s.promised_date).ToList
        End Using

    End Function
    Public Function CustomerList(ByVal CutoffDate As Date) As List(Of String)

        Dim CList As New List(Of String)

        Using Connection As New SqlConnection(ConnectionString)

            Connection.Open()

            Dim sqlString = "select customer from tblSODetails where promiseddate <= '" & CutoffDate.ToShortDateString & "' " &
                "group by customer order by customer"

            Dim command = New SqlCommand(sqlString, Connection)

            Dim dataReader As SqlDataReader = command.ExecuteReader()

            While dataReader.Read()
                MapDataReader(dataReader, 2)
                CList.Add(Customer)
            End While

        End Using

        Return CList

    End Function

    Public Shared Function OpenList(ByVal CutoffDate As Date, ByVal Customer As String, ByVal ShipVia As String) As List(Of SalesOrder)
        Using context = New iTracContext
            Dim query = context.SalesOrders.Include("Inventory").Include("Part").AsQueryable
            query = query.Where(Function(s) s.PromisedDate <= CutoffDate)
            If Not String.IsNullOrEmpty(Customer) Then query = query.Where(Function(s) s.Customer = Customer)
            If Not String.IsNullOrEmpty(ShipVia) Then query = query.Where(Function(s) s.ShipVia = ShipVia)
            Return query.OrderBy(Function(l) l.PromisedDate).ToList
        End Using
    End Function

    Public ReadOnly Property PartNumber As String
        Get
            If Part IsNot Nothing Then
                Return Part.NormalizedPartNumber
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property rtsOk As Integer?
        Get
            If Inventory IsNot Nothing Then
                Return Inventory.rts_ok
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property rtsHold As Integer?
        Get
            If Inventory IsNot Nothing Then
                Return Inventory.rts_hold
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property rtsRejected As Integer?
        Get
            If Inventory IsNot Nothing Then
                Return Inventory.rts_rejected
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property rtsOut As Integer?
        Get
            If Inventory IsNot Nothing Then
                Return Inventory.rts_out
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property rtsInReceving As Integer?
        Get
            If Inventory IsNot Nothing Then
                Return Inventory.rts_receiving
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property rtsInShipping As Integer?
        Get
            If Inventory IsNot Nothing Then
                Return Inventory.rts_in_shipping
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property rtsIssued As Integer?
        Get
            If Inventory IsNot Nothing Then
                Return Inventory.rts_issued
            Else
                Return Nothing
            End If
        End Get
    End Property

End Class

1 个答案:

答案 0 :(得分:0)

此行引发异常:

Private Shared ConnectionString As String = BrowserUtilities.Settings.ConnectionString

表达式BrowserUtilities.Settings.ConnectionString中的某些事件引发异常。目前尚不清楚,但我会说NullReferenceException是最有可能的候选人。也许BrowserUtilities.SettingsNothing。这只是猜测。您必须对其进行调试才能找到答案。


Shared(在C#中为static)字段(例如您代码中的ConnectionString)具有初始化表达式时,该初始化发生在称为“类型初始值设定项”的特殊方法中。类型中Shared字段的所有初始化表达式都被分组到类型初始化器中,并在首次使用该类型之前执行 *

* 为便于说明,这是一个略微的简化。

第一次调用SalesOrdersByBody_JB函数时,它首先必须初始化类型,这意味着运行类型初始值设定项来初始化ConnectionString。看起来SalesOrdersByBody_JB引发了异常,但在此之前确实确实发生了轻微