StuffHappens StuffHappens - 4 months ago 28
C# Question - the Size property has an invalid size of 0

I'm trying to get output value from DB via There's a client code:

using (var connection = new SqlConnection(ConnectionString))
SqlCommand command = new SqlCommand("pDoSomethingParamsRes", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@i", 1);
var outParam = new SqlParameter("@out", SqlDbType.VarChar);
outParam.Direction = ParameterDirection.Output;

When I run this I get the following exception:

the Size property has an invalid size of 0

According to manual SqlParameter.Size Property I might omit size. Why do I get this exception?
How to make it work without passing size?

Thank you for your help!


VarChar and NVarChar are variable width character fields (thus var+char). You have to set the length, otherwise the default is zero.