Mythily
Mythily

Reputation: 57

index out of range exception in c#

This is my 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 "";
            }
        }

This is my Stored Procedure

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

and the output of my SP is

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

In this SP iam getting the min% from the table ISecurityStatistics, subISU and Relationship are master tables to get the subISUName which iam passing as parameters to it.

but in DataAccessLayer iam getting error as "Index out of range exception"

Kindly help me out to fix the issue.. thanks in advance..

Upvotes: 0

Views: 5532

Answers (1)

Christopher Cartlidge
Christopher Cartlidge

Reputation: 186

It's because you are trying to access the value by name even though no name has been set.

Use either:

reader[0].ToString()

Or you could name the column:

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

Then call what you currently do.

Upvotes: 5

Related Questions