473,503 Members | 1,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PropertyInfo SetValue Question

I am trying to write a subroutine that will examine all
the properties of a form to determine which of those are
SqlCommand objects, and then set the Connection property
of those SqlCommands to a passed-in SqlCommand object. I
can find the properties OK but am unsuccessful in trying
to change their value due to the way I have identified
those SqlCommand objects (as a PropertyInfo). Can anyone
give me a hint (or suggest a better way) as I am totally
bamboolzed? I have tried PropertyInfo.SetValue and
MethodInfo.Invoke but all seem to get the same exception
"Object does not match target type".

I have included some sample code:

Public Sub SetDBConnections(ByVal TargetForm As Form,
ByVal DBConnection As SqlClient.SqlConnection)
Dim myType As Type = TargetForm.GetType
Dim myPropertyInfoList As
System.Reflection.PropertyInfo() = myType.GetProperties
((System.Reflection.BindingFlags.NonPublic Or
System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance))
Dim myPropertyInfo As System.Reflection.PropertyInfo
Try
For Each myPropertyInfo In myPropertyInfoList
If myPropertyInfo.PropertyType Is GetType
(SqlClient.SqlCommand) Then
Dim mySQLCmdType As Type =
myPropertyInfo.PropertyType
Dim mySQLCmdPropertyInfoList As
System.Reflection.PropertyInfo() =
mySQLCmdType.GetProperties
((System.Reflection.BindingFlags.NonPublic Or
System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance))
Dim mySQLCmdPropertyInfo As
System.Reflection.PropertyInfo
For Each mySQLCmdPropertyInfo In
mySQLCmdPropertyInfoList
If mySQLCmdPropertyInfo.PropertyType Is
GetType(SqlClient.SqlConnection) Then
Try
''This technique fails
'Dim min As System.Reflection.MethodInfo
= mySQLCmdPropertyInfo.GetSetMethod
'min.Invoke(Me, New
SqlClient.SqlConnection() {DBConnection})

'So does this technique
If mySQLCmdPropertyInfo.CanWrite Then
mySQLCmdPropertyInfo.SetValue
(myPropertyInfo, DBConnection, Nothing)
End If

Exit For
Catch ex As Exception
MessageBox.Show
(ex.Message, "SetDBConnections Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit For
End Try
End If
Next
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "SetDBConnections",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Jul 21 '05 #1
2 7194
Tony,
mySQLCmdPropertyInfo.SetValue(myPropertyInfo, DBConnection, Nothing)


This should probably be

mySQLCmdPropertyInfo.SetValue(myPropertyInfo.GetVa lue(TargetForm,
Nothing), DBConnection, Nothing)

But since AFAICT SqlCommand only has one single property of type
SqlConnection (the Connection property) I'm not sure you you're
setting it in a late bound way in a loop. I'd do

Dim cmd As SqlCommand = DirectCast(myPropertyInfo.GetValue(TargetForm,
Nothing), SqlCommand)
cmd.Connection = DBConnection

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
Hi Mattias,

That worked perfectly - thank you very much!

The reason why I am setting it late bound is that my forms can contain
many SqlCommand objects that are part of the SqlDataAdapters that I have
drop into the component tray. As the command object's connections are
set to my design time values I need to change the values once deployed
to a client's site. On my main startup form I also have a connection
object whose values are changed depending on the site where the
application is being run. I then use this master connection object by
passing it as an argument to each form's Open method, which in turn runs
the subroutine I posted to set the Connection property of each
SqlCommand to work with the client's database. Is this explanation
clear enough?

There might be a better way of doing things but this works well as I use
a lot of form's inheritance and can create systems very quickly.

Anyway, thanks once again for your help.

Tony (Gold Coast, Australia)
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
402
by: Hugo Leonardo | last post by:
I'll big problem. I have tree class: public Class Column { public Column {...} string _Value; public bool HasValue = false;
0
1532
by: Bob | last post by:
This works only when the property return type is string: SomeObjectsPropertyInfo.SetValue(SomeObject, SomeStringValue, Nothing) Is there a good way to try to automatically cast the string value...
15
8184
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
1
3938
by: Paul | last post by:
This part is easy: Dim oControl As Control oControl = Me.Page.FindControl("TextBox1") Dim oPropertyInfo As PropertyInfo oPropertyInfo = oControl.GetType.GetProperty("Text") ...
2
291
by: Tony Tullemans | last post by:
I am trying to write a subroutine that will examine all the properties of a form to determine which of those are SqlCommand objects, and then set the Connection property of those SqlCommands to a...
0
1407
by: tatilou | last post by:
Hello, I need add new method to my properties. these methods should give me some attributes coming from my database. I think, to create a class with implement PropertyInfo but I don't know...
2
2503
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer)...
9
15982
by: FourEyes | last post by:
I've read that Reflection is slow and shouldn't be used when performance is a concern. In my case, performance is definitely a concern, so I was thinking that I could collect the PropertyInfo...
1
4849
by: damiensawyer | last post by:
Hi, I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo objects. Even though they're both derived from MemberInfo, they don't share those two methods. I've finding myself...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.