索引超出范围异常c#

时间:2014-07-25 13:01:02

标签: c# sql-server stored-procedures indexoutofrangeexception

这是我的DataAccessLayer

public string GetMinISecPer(ISecuritySummmary iSecObj)
        {
            try
            {
                string miniSecPer = null;
                SelectCommand cmd = new SelectCommand(SPConst.GetMinIsecPer);
                cmd["@SubISUName"] = iSecObj.SubISUName;
                using (DBDataReaderWrapper reader = cmd.Execute())
                {
                    while (reader.Read())
                    {
                        double miniSecPercent = Convert.ToDouble(reader.String["testValue"]);
                        miniSecPer = Convert.ToString(miniSecPercent);
                    }
                }
                return miniSecPer;
            }
            catch (Exception e)
            {
                return "";
            }
        }

这是我的存储过程

ALTER PROCEDURE [dbo].[GetMinIsecPer]

@SubISUName varchar(max)

AS

BEGIN
DECLARE @MinIsecPer float;
DECLARE @testValue varchar(20);
set @testValue='value';

set @MinIsecPer=(select MIN(iSecComplPer) from ISecurityStatistics IST
 left outer join Relationship R on R.Id=IST.RelationshipId

 left outer join SubISU SI on SI.Id=R.SubISUId

 where SI.SubISUName=@SubISUName and UploadedDate=(SELECT MAX(UploadedDate) FROM ISecurityStatistics))
set @testValue=(select cast(@MinIsecPer as varchar(20)))

select cast(@testValue as varchar(20))
SELECT
    SQL_VARIANT_PROPERTY(@testValue, 'BaseType'),
    SQL_VARIANT_PROPERTY(@testValue, 'Precision'),
    SQL_VARIANT_PROPERTY(@testValue, 'Scale'),
    SQL_VARIANT_PROPERTY(@testValue, 'MaxLength')
END

我的SP的输出是

(No column name)
72
(No column name)    (No column name)    (No column name)    (No column name)
varchar                0                   0                 20

在此SP中,从表ISecurityStatistics获取min%,subISU和Relationship是主表,以获取作为参数传递给它的subISUName。

但是在DataAccessLayer中我得到错误"索引超出范围异常"

请帮我解决问题..提前谢谢..

1 个答案:

答案 0 :(得分:4)

这是因为即使没有设置名称,您也试图按名称访问该值。

使用:

reader[0].ToString()

或者你可以命名专栏:

select cast(@testValue as varchar(20)) As [testValue]

然后打电话给你现在做的事。