IDataParameter - output parameter returns weird results

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Hi,

I'm trying to use a method like so to get values from within an
IDataParameter collection:


protected System.Object GetParameterValue(IDataParameterCollection
parameterCollection, System.String parameterName)
{
System.Object returnValue = null;
foreach(IDataParameter paramMember in parameterCollection)
{
if(paramMember.ParameterName.Equals(parameterName))
{
returnValue = (System.Object) paramMember.Value;
break;
}
}
return returnValue;



}


The problem is that this returns the word "Output" for any output
parameter, it does not return the actual value of the output parameter.

Why is that?

I'd like to keep this method as non-platform specific as possible so
converting it to a SQLParameter object wouldn't work for me.
 
Doug, it must be how you are setting up the parameters, because it
works on my system.

class Program
{
static void Main(string[] args)
{
SqlCommand cmd = new SqlCommand();

SqlParameter param1 = new SqlParameter("@Param1", "first
parameter");
param1.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param1);

SqlParameter param2 = new SqlParameter("@Param2", "second
parameter");
param1.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param2);

SqlParameter param3 = new SqlParameter("@Param3", "third
parameter");
param1.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param3);

SqlParameter param4 = new SqlParameter("@out", "output
parameter");
param1.Direction = ParameterDirection.Output;
cmd.Parameters.Add(param4);

Console.WriteLine(GetParameterValue(cmd.Parameters,
"@out"));
}


protected static System.Object
GetParameterValue(IDataParameterCollection parameterCollection,
System.String parameterName)
{
System.Object returnValue = null;
foreach (IDataParameter paramMember in parameterCollection)
{
if (paramMember.ParameterName.Equals(parameterName))
{
returnValue = (System.Object)paramMember.Value;
break;
}
}
return returnValue;
}
}


Best regards,

Rick
 
That was it. I was missing a parameter value when I created my
Parameter object, I was passing the value of Output as the value of the
Parameter.

boy do I feel dumb. :)
 
Back
Top