Size property has an invalid size of 0

RuMoR picture RuMoR · Dec 25, 2011 · Viewed 54.5k times · Source

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.

Answer

marc_s picture marc_s · Dec 25, 2011

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 ......