Search code examples
c#asp.netsql-serverstored-proceduressqlparameter

Size property has an invalid size of 0


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.


Solution

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