四舍五入数据时的IndexOutOfRangeException

时间:2015-04-21 12:25:33

标签: sql-server vb.net rounding indexoutofrangeexception

我有以下stored procedure

Create Procedure [dbo].[spVerifyCustomerUserLogin]    

@EmbossLine varchar(16),    

@password varchar(50)    

as    

    Select C.EmbossLine,C.EmbossName,round(CB.TotalLoading,2),round(CB.Totalredemption,2),round(CB.CardBalance,2) 
    from Card C inner join CardBalance CB on CB.PAN=C.EmbossLine

    where 

    EmbossLine = @EmbossLine 

    and [password] = @password 

    and Status='E0'

我有以下VB.Net代码来访问它:

Dim UserData As New ArrayList
            Dim dr As SqlDataReader
            dr = GenericDB.ExecuteSPForDataReader("spVerifyCustomerUserLogin", spParameters)
            If dr.HasRows Then
                dr.Read()
                UserData.Add(dr.Item("EmbossLine"))
                UserData.Add(dr.Item("EmbossName"))
                UserData.Add(dr.Item("TotalLoading"))
                UserData.Add(dr.Item("Totalredemption"))
                UserData.Add(dr.Item("CardBalance"))
                dr.Close()
                Return UserData
            End If
            dr.Close()
            Return UserData

问题是我的代码在

上引发异常IndexOutOfRangeException
UserData.Add(dr.Item("TotalLoading"))

请注意,当我在没有round函数的情况下访问存储过程时,它可以正常工作,因此它与我的圆函数有关。 请注意,我的字段TotalLoadingTotalRedemptionCardBalance属于金钱类型。是因为那个吗?

2 个答案:

答案 0 :(得分:2)

不,因为你在totalLoading列上使用了一个函数而且没有别名。

将存储过程中的SELECT语句更改为:

 Select 
  C.EmbossLine,
  C.EmbossName,
  round(CB.TotalLoading,2) as TotalLoading,
  round(CB.Totalredemption,2) as TotalRedemption,
  round(CB.CardBalance,2) as CardBalance
        from Card C inner join CardBalance CB on CB.PAN=C.EmbossLine

答案 1 :(得分:2)

当您对列执行操作(例如round(CB.TotalLoading,2))时,它不再具有名称。您可以使用AS为其命名,例如

SELECT C.EmbossLine, C.EmbossName, ROUND(CB.TotalLoading, 2) AS 'RoundedTotalLoading',...