Size property has an invalid size of 0
Asked Answered
S

4

79

I am working on a social network, one of my procedures returns a VARCHAR output. So this is what I wrote:

SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar);
job1.Direction = ParameterDirection.Output;

However this error comes up:

String[1]: the Size property has an invalid size of 0.

Sloan answered 25/12, 2011 at 11:5 Comment(2)
For anyone with this same problem but for an int or other parameter type, you should specify the DbType of the SqlParameter object. I wasn't doing that and received this same error message.Mychael
OMG! That is a HORRIBLE error message, one of the worst. The least they could do is give the parameter name or stored procedure name in the error message like they do on other errors. I'm so glad you posted, thanks for saving me HUGE amounts of time finding an answer.Military
W
143

You need to define a length when specifying the varchar parameter:

SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar, 50);

You should use the same length as defined in your SQL Server stored procedure.

And btw: if your stored procedure also has no length defined (something like @job VARCHAR OUTPUT) - then you've defined a varchar string of 1 character length ......

Williswillison answered 25/12, 2011 at 11:9 Comment(2)
I have a question because I have the same exception...if the column is varchar, what is the danger of having varchar in your SP and code using DbType.AnsiStringBrufsky
It is known as the "Size" attribute in case you need to specify it like I didVerbality
B
11

Yes, have to define the length for varchar / nvarchar data type like below.

cmd.Parameters.Add("@Description", SqlDbType.VarChar, 150).Direction =
    ParameterDirection.Output;
Bow answered 23/2, 2017 at 12:13 Comment(1)
This doesn't add anything useful to the answer that predated it by 5 years.Sherris
R
5

If you are using Dapper, you have passed in a null value for the parameter, where the expected input was a string.

To find the offending value and it's parameter, inspect at the Dapper DynamicParameters object during a debug session and open the parameters being index referred to.

enter image description here

Rasping answered 26/11, 2020 at 23:52 Comment(0)
I
1

Assign an empty string to the output parameter before calling the stored procedure and that should get rid of the error:

job1.Value = string.Empty;

Ingeborgingelbert answered 21/3, 2022 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.