Google+

Thursday, March 7, 2013

Ado.net - the Size property has an invalid size of 0


I'm trying to get output value from DB via ADO.net. There's a client code:
    using (var connection = new SqlConnection(ConnectionString))
    {
        connection.Open();
        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;
        command.Parameters.Add(outParam);
        command.ExecuteNonQuery();
        Console.WriteLine(command.Parameters["@out"].Value.ToString());
    }
When I run this I get the following exception:
the Size property has an invalid size of 0

Solutions :-

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


outParam.Size= int.MaxValue

to define maximum value of integer.

0 comments:

Post a Comment