Direction = ParameterDirection.Output, ParameterName = "@display", Size = 200, SqlDbType = SqlDbType.NVarChar
Needed to set the size for nvarchar
Question
I created a stored procedure to get return value from a dynamic sql. I get the following exception:
String[1]: the Size property has an invalid size of 0.
My proc:
CREATE proc [dbo].[Review_Get_PrePopValue](@sqlQuery nvarchar(500), @display nvarchar(200) OUTPUT)
as
EXEC sp_executesql @sqlQuery,
@display OUTPUT
My Code:
public string GetAnswerValue(string contentSQL, string parameter, string parameterValue)
{
string sqlstatement = contentSQL.Replace(parameter, parameterValue);
using (var conn = new SqlConnection(_connectionString))
{
string prePopValue;
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "Review_Get_PrePopValue";
comm.Parameters.Add(new SqlParameter("@sqlQuery", SqlDbType.NVarChar)).Value = sqlstatement;
comm.Parameters.Add(new SqlParameter
{
Direction = ParameterDirection.Output,
ParameterName = "@display",
SqlDbType = SqlDbType.NVarChar
});
conn.Open();
comm.ExecuteNonQuery();
prePopValue = comm.Parameters["@display"].Value.ToString();
return prePopValue;
}
}
My sql string:
Select @display = Grant_Number From GMIS_Grants where grant_id=1
Solution
Direction = ParameterDirection.Output, ParameterName = "@display", Size = 200, SqlDbType = SqlDbType.NVarChar
Needed to set the size for nvarchar
OTHER TIPS
From MSDN
For bidirectional and output parameters, and return values, you must set the value of Size.
Also, add this at the end of your SP.
SELECT @display;