变量'SQ_LIB'隐藏了封闭块中的变量

时间:2014-10-16 20:12:46

标签: asp.net variables stored-procedures foreach parameter-passing

我已经搜索了其他论坛,并在每个代码示例中了解了为什么会返回此错误,但无法在下面的代码中查看为什么会返回此错误,因为我只是声明了' intReleaseMasterID'一旦。我试图将在循环外声明和确定的变量传递给循环,并在循环的每次迭代中使用相同的变量 - 可以这样做吗?有什么建议吗?

Protected Sub btnOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOrder.Click
    Dim SQ_LIB As New SQL_Lib
    Dim intReleaseMasterID As Integer
    Dim param(1) As SqlParameter
    param(0) = SQ_LIB.AddParameter("@UserName", Membership.GetUser().UserName)
    param(1) = SQ_LIB.AddParameter("@ReleaseMasterID", Data.ParameterDirection.Output, 0)
    SQ_LIB.RunProcedure("ReleaseMasterToPrimaryLocation", param)
    intReleaseMasterID = CInt(param(1).Value)

    For Each item As GridDataItem In RadGrid1.SelectedItems
        Response.Write(item("InventoryDetailSerialID").Text.ToString())
        Dim strInventoryDetailSerialID As String = item.GetDataKeyValue("InventoryDetailSerialID")
        Dim SQ_LIB As New SQL_Lib
        Dim param(2) As SqlParameter
        param(0) = SQ_LIB.AddParameter("@InventoryDetailSerialID", strInventoryDetailSerialID)
        param(1) = SQ_LIB.AddParameter("@UserName", Membership.GetUser().UserName)
        param(2) = SQ_LIB.AddParameter("@ReleaseMasterID", intReleaseMasterID)
        SQ_LIB.RunProcedure("ReleaseDetailsToPrimaryLocation", param)
    Next
End Sub

我已添加了另一个子程序,因此参数变量不在同一范围内,但CreatePartOrders子程序从不运行或无法从RadGrid1.SelectedItems或我获取信息没有正确传递intReleaseMasterID变量。这是什么?

    Protected Sub btnOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOrder.Click
    Dim SQ_LIB As New SQL_Lib
    Dim intReleaseMasterID As Integer
    Dim param(1) As SqlParameter
    param(0) = SQ_LIB.AddParameter("@UserName", Membership.GetUser().UserName)
    param(1) = SQ_LIB.AddParameter("@ReleaseMasterID", Data.ParameterDirection.Output, 0)
    SQ_LIB.RunProcedure("ReleaseMasterToPrimaryLocation", param)
    intReleaseMasterID = CInt(param(1).Value)

    CreatePartOrders(intReleaseMasterID)
End Sub
___________________________________________________________
Public Sub CreatePartOrders(ByVal intReleaseMasterID As Integer)
    For Each item As GridDataItem In RadGrid1.SelectedItems
        Response.Write(item("InventoryDetailSerialID").Text.ToString())
        Dim strInventoryDetailSerialID As String = item.GetDataKeyValue("InventoryDetailSerialID")
        Dim SQ_LIB As New SQL_Lib
        Dim param(2) As SqlParameter
        param(0) = SQ_LIB.AddParameter("@InventoryDetailSerialID", strInventoryDetailSerialID)
        param(1) = SQ_LIB.AddParameter("@UserName", Membership.GetUser().UserName)
        param(2) = SQ_LIB.AddParameter("@ReleaseMasterID", intReleaseMasterID)
        SQ_LIB.RunProcedure("ReleaseDetailsToPrimaryLocation", param)
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

您正在两个地方创建SQL_Lib,标记如下......

Protected Sub btnOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOrder.Click
    Dim SQ_LIB As New SQL_Lib **HERE is number 1**
    Dim intReleaseMasterID As Integer
    Dim param(1) As SqlParameter
    param(0) = SQ_LIB.AddParameter("@UserName", Membership.GetUser().UserName)
    param(1) = SQ_LIB.AddParameter("@ReleaseMasterID", Data.ParameterDirection.Output, 0)
    SQ_LIB.RunProcedure("ReleaseMasterToPrimaryLocation", param)
    intReleaseMasterID = CInt(param(1).Value)

    For Each item As GridDataItem In RadGrid1.SelectedItems
        Response.Write(item("InventoryDetailSerialID").Text.ToString())
        Dim strInventoryDetailSerialID As String = item.GetDataKeyValue("InventoryDetailSerialID")
        Dim SQ_LIB As New SQL_Lib **HERE is number 2**
        Dim param(2) As SqlParameter
        param(0) = SQ_LIB.AddParameter("@InventoryDetailSerialID", strInventoryDetailSerialID)
        param(1) = SQ_LIB.AddParameter("@UserName", Membership.GetUser().UserName)
        param(2) = SQ_LIB.AddParameter("@ReleaseMasterID", intReleaseMasterID)
        SQ_LIB.RunProcedure("ReleaseDetailsToPrimaryLocation", param)
    Next
End Sub

你正在使用Dim和As来声明它两次。