I am writing an output stored procedure that i access using c#. I am trying to print the output values from the stored procedure.

using (SqlConnection connection = new SqlConnection(connectionstring))
        {
            connection.Open();            
            SqlCommand emailcmdsql;
            emailcmdsql = new SqlCommand("returnemail", connection);
            emailcmdsql.CommandType = CommandType.StoredProcedure;
            SqlParameter paruser = emailcmdsql.Parameters.Add("@user", SqlDbType.VarChar);
            paruser.Value = username.Text;
            paruser.Direction = ParameterDirection.Input;
            SqlParameter paremail = emailcmdsql.Parameters.Add("@email", SqlDbType.VarChar);
            paremail.Direction = ParameterDirection.Output;
            SqlParameter parpassword = emailcmdsql.Parameters.Add("@password", SqlDbType.VarChar);
            parpassword.Direction = ParameterDirection.Output;
                emailcmdsql.ExecuteNonQuery();
                string email = paremail.Value.ToString();
                string password = parpassword.Value.ToString();
                MessageBox.Show(email);
                MessageBox.Show(password);                  
        connection.Close();
}

below is the stored procedure

Alter PROCEDURE [dbo].returnemail
	@user varchar(30),
	@email varchar(50) OUTPUT,
	@password varchar (20) OUTPUT	
AS
BEGIN
	SET NOCOUNT ON;
  	SELECT @email = email, @password = password from ESS where username = @user
END

Please tell me what i'm doing wrong. I am getting this error.

String[1]: the Size property has an invalid size of 0.

You have to specify the size argument.

SqlParameter paruser = emailcmdsql.Parameters.Add("@user", SqlDbType.VarChar,30);
....
SqlParameter paremail = emailcmdsql.Parameters.Add("@email", SqlDbType.VarChar,50);
...
SqlParameter parpassword = emailcmdsql.Parameters.Add("@password", SqlDbType.VarChar,30);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.