467,145 Members | 1,015 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,145 developers. It's quick & easy.

Hot to set object values from stored proc parameters?

I'm trying to iterate stored parameters and populate any public properties of
an object with the parameter value. I've suceeded in doing it the other way
around, but when I try it to the object I keep getting the error "Object does
not match target type".

I've tried field.SetValue(p, p.Value, BindingFlags.SetField, null, null,
null) and a couple variations without sucess.

Thanks in advance for any help you can offer.

public static void PopulateRecordFromProcedureParameters(Object Record,
SqlCommand cmd)
{
PropertyInfo field = null;

foreach (SqlParameter p in cmd.Parameters)
{
if (p.Direction != ParameterDirection.Input)
{
field =
Record.GetType().GetProperty(p.ParameterName.Repla ce("@", ""));
// Here's where I need to set the value
field.SetValue(p, p.Value, BindingFlags.SetField, null, null, null);
}
}

} // PopulateRecordFromProcedureParameters

Jun 14 '06 #1
  • viewed: 1358
Share:
3 Replies
Hi,

You have to make sure that both p.Value and the target property are of the
same type.

Even more, you need to check if the property has a set member.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Byron" <By***@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
I'm trying to iterate stored parameters and populate any public properties
of
an object with the parameter value. I've suceeded in doing it the other
way
around, but when I try it to the object I keep getting the error "Object
does
not match target type".

I've tried field.SetValue(p, p.Value, BindingFlags.SetField, null, null,
null) and a couple variations without sucess.

Thanks in advance for any help you can offer.

public static void PopulateRecordFromProcedureParameters(Object Record,
SqlCommand cmd)
{
PropertyInfo field = null;

foreach (SqlParameter p in cmd.Parameters)
{
if (p.Direction != ParameterDirection.Input)
{
field =
Record.GetType().GetProperty(p.ParameterName.Repla ce("@", ""));
// Here's where I need to set the value
field.SetValue(p, p.Value, BindingFlags.SetField, null, null, null);
}
}

} // PopulateRecordFromProcedureParameters

Jun 14 '06 #2
I realize I have to cast the value from the stored procedure, but I'm not
having any success in doing so. Since I'm trying to match a parameter with
its corresponding property iteratively I can't know the types at design time
so I need to know how to cast a parameter value to the type of an arbitrary
property at runtime. What wold I use in place of (CAST AS field TYPE) to
cast the value of the parameter to the type of the field?

field.SetValue(Record, (CAST AS field TYPE)p.Value, null);
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

You have to make sure that both p.Value and the target property are of the
same type.

Even more, you need to check if the property has a set member.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Byron" <By***@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
I'm trying to iterate stored parameters and populate any public properties
of
an object with the parameter value. I've suceeded in doing it the other
way
around, but when I try it to the object I keep getting the error "Object
does
not match target type".

I've tried field.SetValue(p, p.Value, BindingFlags.SetField, null, null,
null) and a couple variations without sucess.

Thanks in advance for any help you can offer.

public static void PopulateRecordFromProcedureParameters(Object Record,
SqlCommand cmd)
{
PropertyInfo field = null;

foreach (SqlParameter p in cmd.Parameters)
{
if (p.Direction != ParameterDirection.Input)
{
field =
Record.GetType().GetProperty(p.ParameterName.Repla ce("@", ""));
// Here's where I need to set the value
field.SetValue(p, p.Value, BindingFlags.SetField, null, null, null);
}
}

} // PopulateRecordFromProcedureParameters


Jun 14 '06 #3
"Byron" <By***@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
I realize I have to cast the value from the stored procedure, but I'm not
having any success in doing so. Since I'm trying to match a parameter
with
its corresponding property iteratively I can't know the types at design
time
so I need to know how to cast a parameter value to the type of an
arbitrary
property at runtime. What wold I use in place of (CAST AS field TYPE) to
cast the value of the parameter to the type of the field?

field.SetValue(Record, (CAST AS field TYPE)p.Value, null);
either TypeDescriptor.GetConverter(p.Value.GetType()).Con vertTo(p.Value,
field.FieldType)
or TypeDescriptor.GetConverter(field.FieldType).Conve rtFrom(p.Value)


"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

You have to make sure that both p.Value and the target property are of
the
same type.

Even more, you need to check if the property has a set member.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Byron" <By***@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
> I'm trying to iterate stored parameters and populate any public
> properties
> of
> an object with the parameter value. I've suceeded in doing it the
> other
> way
> around, but when I try it to the object I keep getting the error
> "Object
> does
> not match target type".
>
> I've tried field.SetValue(p, p.Value, BindingFlags.SetField, null,
> null,
> null) and a couple variations without sucess.
>
> Thanks in advance for any help you can offer.
>
> public static void PopulateRecordFromProcedureParameters(Object Record,
> SqlCommand cmd)
> {
> PropertyInfo field = null;
>
> foreach (SqlParameter p in cmd.Parameters)
> {
> if (p.Direction != ParameterDirection.Input)
> {
> field =
> Record.GetType().GetProperty(p.ParameterName.Repla ce("@", ""));
> // Here's where I need to set the value
> field.SetValue(p, p.Value, BindingFlags.SetField, null, null, null);
> }
> }
>
> } // PopulateRecordFromProcedureParameters
>


Jun 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tavish Muldoon | last post: by
11 posts views Thread by ColdCanuck | last post: by
5 posts views Thread by Paul Aspinall | last post: by
8 posts views Thread by Jiggaz | last post: by
6 posts views Thread by David Lozzi | last post: by
reply views Thread by Elliot M. Rodriguez | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.