从webmethod

时间:2016-02-15 20:47:56

标签: asp.net vb.net webmethod

我有一个<WebMethod> PassPLU,由angularjs $http.post调用。然后<WebMethod>调用另一个Sub LoadData。在Sub的末尾,它会填充asp:HiddenField。不幸的是,这会导致nullreferenceexception。但是,LoadData Sub也会被asp:Button直接调用,效果很好。

以下是<WebMethod>Sub

<System.Web.Services.WebMethod()>
Public Shared Sub PassPLU(ByVal pluno As String, ByVal deptname As String)
    Dim currentPage As New plu
    currentPage.LoadData(pluno, deptname)
End Sub

Protected Sub LoadData(ByVal pluno As String, Optional ByVal deptname As String = Nothing)
    Dim productData As New Product
    Dim sqlStr As String

    connection.Open()
    If deptname = Nothing Then
        sqlStr = String.Format("SELECT * FROM PWMv_PLUM_PLUMaster AS a, PWMv_PLUM_Departments AS b WHERE a.pluno = {0} AND a.deptno = b.deptno;", pluno)
    Else
        sqlStr = String.Format("SELECT * FROM PWMv_PLUM_PLUMaster AS a, PWMv_PLUM_Departments AS b WHERE a.pluno = {0} AND b.deptname = '{1}';", pluno, deptname)
    End If
    Dim command As SqlCommand = New SqlCommand(sqlStr, connection)
    Dim pluData As SqlDataReader = command.ExecuteReader()
    While pluData.Read()
        productData.PLU = pluData("pluno")
        productData.Dept = pluData("deptname")
        productData.DeptNo = pluData("deptno")
        productData.Desc1 = pluData("desc1")
        If Not IsDBNull(pluData("desc2")) Then
            productData.Desc2 = pluData("desc2")
        End If
        productData.UPC = pluData("upc")
    End While
    pluData.Close()

    sqlStr = String.Format("SELECT ingrtext FROM PWMv_PLUM_IngrMaster WHERE ingrno = {0} AND deptno = {1};", pluno, productData.DeptNo)
    command.CommandText = sqlStr
    productData.Ingredients = command.ExecuteScalar()

    sqlStr = String.Format("SELECT * FROM PWMv_PLUM_NutriMaster WHERE nutrino = {0} AND deptno = {1};", pluno, productData.DeptNo)
    command.CommandText = sqlStr
    pluData = command.ExecuteReader()
    While pluData.Read()
        productData.ServSize = pluData("serveuomdesc")
        If Not IsDBNull(pluData("servpercon")) Then
            productData.ServPer = pluData("servpercon")
        End If
        productData.Calories = pluData("calories") / 10
        productData.CaloriesFat = pluData("calfrmfat") / 10
        productData.TotalFat = pluData("totalfat") / 10
        productData.PerFat = pluData("totalfatper") / 10
        productData.SatFat = pluData("saturfat") / 10
        productData.PerSatFat = pluData("saturfatper") / 10
        productData.TransFat = pluData("transfat") / 10
        productData.Cholesterol = pluData("cholesterol") / 10
        productData.PerCholesterol = pluData("cholesterolper") / 10
        productData.Sodium = pluData("sodium") / 10
        productData.PerSodium = pluData("sodiumper") / 10
        productData.Carbs = pluData("totalcarb") / 10
        productData.PerCarbs = pluData("totalcarbper") / 10
        productData.Fiber = pluData("dietfiber") / 10
        productData.PerFiber = pluData("dietfiberper") / 10
        productData.Sugars = pluData("sugars") / 10
        productData.Protein = pluData("protein") / 10
        productData.VitA = pluData("vitamina") / 10
        productData.VitC = pluData("vitaminc") / 10
        productData.Calc = pluData("calcium") / 10
        productData.Iron = pluData("iron") / 10
        productData.VitD = pluData("vitamind") / 10
        productData.VitE = pluData("vitamine") / 10
        productData.Thia = pluData("thiamine") / 10
        productData.Ribo = pluData("riboflavin") / 10
        productData.Niac = pluData("niacin") / 10
        productData.VitB6 = pluData("vitaminb6") / 10
        productData.Folate = pluData("folate") / 10
        productData.VitB12 = pluData("vitaminb12") / 10
        productData.Biotin = pluData("biotin") / 10
        productData.Phos = pluData("phosphorus") / 10
        productData.Zinc = pluData("zinc") / 10
        productData.Mag = pluData("magnesium") / 10
        productData.Iodine = pluData("iodine") / 10
        productData.Copper = pluData("copper") / 10
        productData.Acid = pluData("pantothenicacid") / 10
    End While
    pluData.Close()
    connection.Close()

    Dim serializer As New JavaScriptSerializer(New SimpleTypeResolver())
    nutritionData.Value = serializer.Serialize(productData)

    Dim builder As StringBuilder = New StringBuilder()
    builder.Append("window.onload = function () {")
    builder.AppendLine("angular.element(document.getElementById('pluPicker')).scope().loadData();")
    builder.AppendLine("}")
    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "loadData", builder.ToString(), True)
End Sub

有人可以解释为什么我会收到这个例外吗?我不明白为什么它以一种方式而不是另一种方式起作用。

更新:异常数据

  

{&#34;对象引用未设置为对象的实例。&#34;}
  在\ pignet2012 \ e \ pignetprograms \ production \ plum2 \ plu.aspx.vb中的plu.LoadData(String pluno,String deptname):第305行      在\ pignet2012 \ e \ pignetprograms \ production \ plum2 \ plu.aspx.vb中的plu.PassPLU(String pluno,String deptname):第225行

0 个答案:

没有答案