473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
3 1500
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tavish Muldoon | last post by:
What a pain trying to insert data into a table from a stored proc. My webform asks for 16 pieces of data - which then gets written to the database. I found this easier than the crap below...
2
by: Stefan Berglund | last post by:
I'm having difficulty making a stored procedure with multiple input parameters work as a native method of the connection object. The only way I could get it to work was as below and I'm wondering...
11
by: ColdCanuck | last post by:
Greetings! I am VERY new to DB2 but not Orable, Sybase and SQL Server. I am trying to call a stored procedure via VB 6 and ADO/OLEDB. But when I try to execute
5
by: Paul Aspinall | last post by:
Hi I have a Stored Proc in SQL server, which creates a record key when a record is created. I want to return the value back to my code, once the record has been created. I am using SQLHelper...
8
by: Jiggaz | last post by:
Hi, In my ASPX Page, i have a form for signup. And whene user click on the button, the event Button1_Click must use a stored procedure. But instead of use stored proc, i get this exception :...
6
by: David Lozzi | last post by:
Here is the proc: CREATE PROCEDURE . @CID as int, @Netname as nvarchar(25), @Return as int OUTPUT AS IF EXISTS (SELECT DISTINCT netname FROM computers WHERE CompanyID = @CID AND...
7
by: Peter D.C. | last post by:
Hi I want to update data hold in several textbox controls on an asp.net form. But it seems like it is the old textbox values that is "re-updates" through a stored procedure who updates a SQL...
1
by: Mok | last post by:
Hello, I want to pull information from a table in sql server, bind it to a grid and update those values. I would like to know the best way to do it. I used the configure data adapter wizard and it...
0
by: Elliot M. Rodriguez | last post by:
I implemented a very small, basic data access layer for my web application. It works just fine, except for this one bug. One of my methods returns an abstracted dataset. To accomodate X number of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.