Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Exception : The Size Property Has an Invalid Size Of 0

i have created a method with a following signature;

public bool CheckEmployeeRegistrationNo(string registrationNo, DateTime timeIn, int createdBy,
    DateTime createdDate, int updatedBy, DateTime updatedDate)
{
    ClsDatabaseManager dbManager = ClsDatabaseManager.InitializeDbManager();

    dbManager.CreateParameters(9);
    dbManager.AddParameters(0, "@RegistrationNo", registrationNo);
    dbManager.AddParameters(1, "@CreatedBy", createdBy);
    dbManager.AddParameters(2, "@CreatedDate", createdDate);
    dbManager.AddParameters(3, "@UpdatedBy", updatedBy);
    dbManager.AddParameters(4, "@UpdatedDate", updatedDate);
    dbManager.AddParameters(5, "@TimeIn", timeIn);
    dbManager.AddParameters(6, "@Name", EmployeeName, System.Data.ParameterDirection.Output);
    dbManager.AddParameters(7, "@GeneratedTimeIn", GeneratedTimeIn, System.Data.ParameterDirection.Output);
    dbManager.AddParameters(8, "@ImageUrl", ImageUrl, System.Data.ParameterDirection.Output);

    int result = -1;
    try
    {
        dbManager.Open();
        result = dbManager.ExecuteNonQuery("usp_EmployeeCheckRegNo");
        if (result > 0)
        {
            EmployeeName = dbManager.Parameters[6].Value.ToString();
            GeneratedTimeIn = dbManager.Parameters[7].Value.ToDate();
            ImageUrl = dbManager.Parameters[8].Value.ToString();
        }
        dbManager.Dispose();
    }
    catch (Exception ex)
    {
        dbManager.Dispose();
        throw ex;
    }

    return (result != -1);

}

While my stored procedure looks like;

ALTER PROC [dbo].[usp_EmployeeCheckRegNo]
(
    @RegistrationNo nvarchar(50),
    @TimeIn Datetime,
    @CreatedBy INT,
    @CreatedDate datetime,
    @UpdatedBy INT,
    @UpdatedDate datetime,
    @Name nvarchar(50) OUT,
    @GeneratedTimeIn datetime OUT,
    @ImageUrl nvarchar(100) OUT
)
AS

BEGIN

if Exists 
(
    select RegistationNo 
    FROM [Employe].Registration
    WHERE [RegistationNo] = @RegistrationNo
)
    BEGIN
        INSERT INTO [Employe].[Attendance]
        (
            RegistrationNo,
            [TimeIn],
            [CreatedBy],
            [CreatedDate],
            [UpdatedBy],
            [UpdatedDate]
        )
        VALUES
        (
            @RegistrationNo,
            @TimeIn,
            @CreatedBy,
            @CreatedDate,
            @UpdatedBy,
            @UpdatedDate
        )
         SET @Name = (select er.Name FROM [Employe].[Registration] er
            WHERE er.[RegistationNo] = @RegistrationNo);
         SET @GeneratedTimeIn = (select a.TimeIn 
            FROM [Employe].Attendance a
            WHERE a.RegistrationNo = @RegistrationNo);
         SET @ImageUrl = (select er.[Image] 
            FROM [Employe].[Registration] er
            WHERE er.RegistationNo = @RegistrationNo);
    END
ELSE
    return 0
END

However, when i enter 2323 (value) in a textbox, and click on a button (on .aspx), i am getting an error. I also debug in visual studio.
The error is; String[6]: The Size Property Has an Invalid Size Of 0
Why i am getting this error at String 6 ?
Note: i am currently supplying null values to the parameters 6, 7, 8 becuase they are returned as outparamter. Visual Studio Screen Shot

While in sql server, i run the stored procedure and output the expected results;
enter image description here

like image 362
Idrees Khan Avatar asked Feb 25 '13 07:02

Idrees Khan


1 Answers

I don't know about the ClsDatabaseManager class you are using but with the SqlParameterCollection type of the SqlCommand.Parameters property, you have to specify a size when calling the Add() function for nvarchar parameters.

Thus I would say that you should be able to do something like:

dbManager.AddParameters(0, "@RegistrationNo", registrationNo, 50);

to specify the same string length as your stored procedure.

like image 166
Yannick Blondeau Avatar answered Oct 19 '22 22:10

Yannick Blondeau