如何在ASP.net中使用数字数据类型更新Boundedfield?

时间:2018-10-09 10:54:50

标签: asp.net vb.net

我有以下VB.net代码,使用标识的单元号从Boundedfields更新行。

Protected Sub grvPos_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles grvPos.RowUpdating

    Dim cs As String
    Dim con As iDB2Connection
    Dim sql As String
    Dim cmd As iDB2Command
    Dim valDate As Integer
    Dim portCode As String
    Dim secCode As String
    Dim valType As String
    Dim portName As String
    Dim ISINno As String
    Dim secName As String
    Dim secCcyAbbr As String
    Dim trxBccy As Decimal
    Dim quantity As Integer
    Dim mktPrice As Integer
    Dim avgCost As Integer
    Dim avgBvalSc As Integer
    Dim avgBvalBc As Integer
    Dim intAmtSc As Integer
    Dim intAmtBc As Integer
    Dim gr As GridViewRow

    gr = grvPos.Rows(e.RowIndex)

    valDate = grvPos.DataKeys(gr.RowIndex).Values("VALN_DATE")
    portCode = grvPos.DataKeys(gr.RowIndex).Values("PORT_CODE").ToString()
    secCode = grvPos.DataKeys(gr.RowIndex).Values("SEC_CODE").ToString()
    valType = grvPos.Rows(gr.RowIndex).Cells(2).ToString()
    portName = grvPos.Rows(gr.RowIndex).Cells(4).ToString()
    ISINno = grvPos.Rows(gr.RowIndex).Cells(6).ToString()
    secName = grvPos.Rows(gr.RowIndex).Cells(7).ToString()
    secCcyAbbr = grvPos.Rows(gr.RowIndex).Cells(8).ToString()
    trxBccy = grvPos.Rows(gr.RowIndex).Cells(9).ToString()
    quantity = CInt(grvPos.Rows(gr.RowIndex).Cells(10).ToString())
    mktPrice = CInt(grvPos.Rows(gr.RowIndex).Cells(11).ToString())
    avgCost = CInt(grvPos.Rows(gr.RowIndex).Cells(14).ToString())
    avgBvalSc = CInt(grvPos.Rows(gr.RowIndex).Cells(15).ToString())
    avgBvalBc = CInt(grvPos.Rows(gr.RowIndex).Cells(16).ToString())
    intAmtSc = CInt(grvPos.Rows(gr.RowIndex).Cells(17).ToString())
    intAmtBc = CInt(grvPos.Rows(gr.RowIndex).Cells(18).ToString())

    cs = ConfigurationManager.ConnectionStrings("ConnectionStringDB2").ConnectionString
    con = New iDB2Connection(cs)
    sql = "MERGE INTO GICPFDTA.OPTR_POS_FIX O " &
          "USING OLYFPRO.FDBVAL F ON O.SEC_CODE = F.VLVALR " &
          "AND (O.VALN_DATE = @VALN_DATE) " &
          "AND (O.PORT_CODE = @PORT_CODE) " &
          "AND (O.SEC_CODE = @SEC_CODE) " &
          "WHEN MATCHED THEN UPDATE SET " &
          "O.VAL_TYPE = @VAL_TYPE " &
          ",O.PORT_SNAME = @PORT_SNAME " &
          ",O.ISIN_NO = @ISIN_NO " &
          ",O.SEC_SNAME = @SEC_SNAME " &
          ",O.SEC_CCY_ABBR = @SEC_CCY_ABBR " &
          ",O.TRX_BCCY_EX_RATE = @TRX_BCCY_EX_RATE " &
          ",O.QUANTITY = @QUANTITY " &
          ",O.MKT_PRICE = @MKT_PRICE " &
          ",O.AVG_COST = @AVG_COST " &
          ",O.AVG_BVAL_SC = @AVG_BVAL_SC " &
          ",O.AVG_BVAL_BC = @AVG_BVAL_BC " &
          ",O.INT_AMT_SC = @INT_AMT_SC " &
          ",O.INT_AMT_BC = @INT_AMT_BC " &
          ",O.MVAL_AMT_SC = ROUND(@QUANTITY * @MKT_PRICE / (" &
          "CASE " &
          "WHEN F.VLGTI = 100 " &
          "THEN 100 " &
            "ELSE 1 " &
          "END" &
           "), 3) " &
           ",O.MVAL_AMT_BC = ROUND(@QUANTITY * @MKT_PRICE / (" &
          "CASE " &
          "WHEN F.VLGTI = 100 " &
            "THEN 100 " &
          "ELSE 1 " &
          "END" &
          ") / @TRX_BCCY_EX_RATE, 3)"
    cmd = New iDB2Command(sql, con)
    cmd.Parameters.AddWithValue("@VALN_DATE", valDate)
    cmd.Parameters.AddWithValue("@PORT_CODE", portCode)
    cmd.Parameters.AddWithValue("@SEC_CODE", secCode)
    cmd.Parameters.AddWithValue("@VAL_TYPE", valType)
    cmd.Parameters.AddWithValue("@PORT_SNAME", portName)
    cmd.Parameters.AddWithValue("@ISIN_NO", ISINno)
    cmd.Parameters.AddWithValue("@SEC_SNAME", secName)
    cmd.Parameters.AddWithValue("@SEC_CCY_ABBR", secCcyAbbr)
    cmd.Parameters.AddWithValue("@TRX_BCCY_EX_RATE", trxBccy)
    cmd.Parameters.AddWithValue("@TRX_BCCY_EX_RATE", trxBccy)
    cmd.Parameters.AddWithValue("@QUANTITY", quantity)
    cmd.Parameters.AddWithValue("@QUANTITY", quantity)
    cmd.Parameters.AddWithValue("@QUANTITY", quantity)
    cmd.Parameters.AddWithValue("@MKT_PRICE", mktPrice)
    cmd.Parameters.AddWithValue("@MKT_PRICE", mktPrice)
    cmd.Parameters.AddWithValue("@MKT_PRICE", mktPrice)
    cmd.Parameters.AddWithValue("@AVG_COST", avgCost)
    cmd.Parameters.AddWithValue("@AVG_BVAL_SC", avgBvalSc)
    cmd.Parameters.AddWithValue("@AVG_BVAL_BC", avgBvalBc)
    cmd.Parameters.AddWithValue("@INT_AMT_SC", intAmtSc)
    cmd.Parameters.AddWithValue("@INT_AMT_BC", intAmtBc)

    Try
        lblError.Text = ""

        Using con
            con.Open()
            cmd.ExecuteNonQuery()
            gridLoad()
        End Using

    Catch ex As Exception
        Throw
    End Try
End Sub

trxBccy变量是数字数据类型,但是我将其指定为Decimal,我也尝试了Integer数据类型。 当我尝试更新一行时,出现以下错误:

从字符串“ System.Web.UI.WebControls.DataCo”到“十进制”类型的转换无效。

感谢您的帮助。 预先感谢

1 个答案:

答案 0 :(得分:0)

我能够使用以下代码从有界字段中捕获值。

    Protected Sub grvPos_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles grvPos.RowUpdating

    Dim cs As String
    Dim con As iDB2Connection
    Dim sql As String
    Dim cmd As iDB2Command
    Dim valDate As Integer
    Dim portCode As String
    Dim secCode As String
    Dim valType As String
    Dim portName As String
    Dim ISINno As String
    Dim secName As String
    Dim secCcyAbbr As String
    Dim trxBccy As Decimal
    Dim quantity As Decimal
    Dim mktPrice As Decimal
    Dim avgCost As Decimal
    Dim avgBvalSc As Decimal
    Dim avgBvalBc As Decimal
    Dim intAmtSc As Decimal
    Dim intAmtBc As Decimal
    Dim gr As GridViewRow

    gr = grvPos.Rows(e.RowIndex)



    valDate = grvPos.DataKeys(gr.RowIndex).Values("VALN_DATE")
    portCode = grvPos.DataKeys(gr.RowIndex).Values("PORT_CODE").ToString()
    secCode = grvPos.DataKeys(gr.RowIndex).Values("SEC_CODE").ToString()
    valType = e.NewValues("VAL_TYPE").ToString()
    portName = e.NewValues("PORT_SNAME").ToString()
    ISINno = e.NewValues("ISIN_NO").ToString()
    secName = e.NewValues("SEC_SNAME").ToString()
    secCcyAbbr = e.NewValues("SEC_CCY_ABBR").ToString()
    trxBccy = e.NewValues("TRX_BCCY_EX_RATE").ToString()
    quantity = e.NewValues("QUANTITY").ToString()
    mktPrice = e.NewValues("MKT_PRICE").ToString()
    avgCost = e.NewValues("AVG_COST").ToString()
    avgBvalSc = e.NewValues("AVG_BVAL_SC").ToString()
    avgBvalBc = e.NewValues("AVG_BVAL_BC").ToString()
    intAmtSc = e.NewValues("INT_AMT_SC").ToString()
    intAmtBc = e.NewValues("INT_AMT_BC").ToString()

    cs = ConfigurationManager.ConnectionStrings("ConnectionStringDB2").ConnectionString
    con = New iDB2Connection(cs)
    sql = "MERGE INTO GICPFDTA.OPTR_POS_FIX O " &
          "USING OLYFPRO.FDBVAL F ON O.SEC_CODE = F.VLVALR " &
          "AND (O.VALN_DATE = @VALN_DATE) " &
          "AND (O.PORT_CODE = @PORT_CODE) " &
          "AND (O.SEC_CODE = @SEC_CODE) " &
          "WHEN MATCHED THEN UPDATE SET " &
          "O.VAL_TYPE = @VAL_TYPE " &
          ",O.PORT_SNAME = @PORT_SNAME " &
          ",O.ISIN_NO = @ISIN_NO " &
          ",O.SEC_SNAME = @SEC_SNAME " &
          ",O.SEC_CCY_ABBR = @SEC_CCY_ABBR " &
          ",O.TRX_BCCY_EX_RATE = @TRX_BCCY_EX_RATE " &
          ",O.QUANTITY = @QUANTITY " &
          ",O.MKT_PRICE = @MKT_PRICE " &
          ",O.AVG_COST = @AVG_COST " &
          ",O.AVG_BVAL_SC = @AVG_BVAL_SC " &
          ",O.AVG_BVAL_BC = @AVG_BVAL_BC " &
          ",O.INT_AMT_SC = @INT_AMT_SC " &
          ",O.INT_AMT_BC = @INT_AMT_BC " &
          ",O.MVAL_AMT_SC = ROUND(@QUANTITY * @MKT_PRICE / (" &
          "CASE " &
          "WHEN F.VLGTI = 100 " &
          "THEN 100 " &
            "ELSE 1 " &
          "END" &
           "), 3) " &
           ",O.MVAL_AMT_BC = ROUND(@QUANTITY * @MKT_PRICE / (" &
          "CASE " &
          "WHEN F.VLGTI = 100 " &
            "THEN 100 " &
          "ELSE 1 " &
          "END" &
          ") / @TRX_BCCY_EX_RATE, 3)"
    cmd = New iDB2Command(sql, con)
    cmd.Parameters.AddWithValue("@VALN_DATE", valDate)
    cmd.Parameters.AddWithValue("@PORT_CODE", portCode)
    cmd.Parameters.AddWithValue("@SEC_CODE", secCode)
    cmd.Parameters.AddWithValue("@VAL_TYPE", valType)
    cmd.Parameters.AddWithValue("@PORT_SNAME", portName)
    cmd.Parameters.AddWithValue("@ISIN_NO", ISINno)
    cmd.Parameters.AddWithValue("@SEC_SNAME", secName)
    cmd.Parameters.AddWithValue("@SEC_CCY_ABBR", secCcyAbbr)
    cmd.Parameters.AddWithValue("@TRX_BCCY_EX_RATE", trxBccy)
    cmd.Parameters.AddWithValue("@TRX_BCCY_EX_RATE", trxBccy)
    cmd.Parameters.AddWithValue("@QUANTITY", quantity)
    cmd.Parameters.AddWithValue("@QUANTITY", quantity)
    cmd.Parameters.AddWithValue("@QUANTITY", quantity)
    cmd.Parameters.AddWithValue("@MKT_PRICE", mktPrice)
    cmd.Parameters.AddWithValue("@MKT_PRICE", mktPrice)
    cmd.Parameters.AddWithValue("@MKT_PRICE", mktPrice)
    cmd.Parameters.AddWithValue("@AVG_COST", avgCost)
    cmd.Parameters.AddWithValue("@AVG_BVAL_SC", avgBvalSc)
    cmd.Parameters.AddWithValue("@AVG_BVAL_BC", avgBvalBc)
    cmd.Parameters.AddWithValue("@INT_AMT_SC", intAmtSc)
    cmd.Parameters.AddWithValue("@INT_AMT_BC", intAmtBc)

    Try
        lblError.Text = ""

        Using con
            con.Open()
            cmd.ExecuteNonQuery()
            grvPos.EditIndex = -1
            gridLoad()
        End Using

    Catch ex As Exception
        Throw
    End Try
End Sub