473,789 Members | 2,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

field validation with reflection

Dan
hi newsgroup,

i need server side validation of user entries.
as i do have many fields, i would like to use reflection for checkinng
the IsValid property of all the Validators.
here is the reflection code

i don't understand why i get the following error:
Object does not match target type.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.Reflecti on.TargetExcept ion: Object does not
match target type.

at this line: object
ret=field.Field Type.GetPropert y("IsValid",typ eof(bool)).GetV alue(field,null );

the code is a loop the goes through all public fields of the user control:
public static bool checkValidators (Control page)
{
bool isValid=true;
FieldInfo[] fields=page.Get Type().GetField s();
foreach(FieldIn fo field in fields)
{

if(field.FieldT ype.IsSubclassO f(typeof(System .Web.UI.WebCont rols.BaseValida tor)))
{

object
ret=field.Field Type.GetPropert y("IsValid",typ eof(bool)).GetV alue(field,null );
isValid=Convert .ToBoolean(ret) ;
}
}
return isValid;
}

any ideas?
or maye there is another way to check the IsValid property of all the
validators in a fast way?
thanks for your help.
dan
Nov 18 '05 #1
2 1472
> or maye there is another way to check the IsValid property of all the
validators in a fast way?

I could have sorted your reflection problem, but I think Page.IsValid is
what you should use. This method returns true if all validators on a page
return true, otherwise it returns false.

Anders Norås
blog: http://dotnetjunkies.com/weblog/anoras/
Nov 18 '05 #2
If you want to find out more information that just Page.IsValid (such as
which validators failed or the messages that they contain) this should
get you on the right track (watch for word wrap):

foreach (Control currentControl in this.Page.Valid ators)
{
IValidator currentValidato r = currentControl as IValidator;
if (currentValidat or != null)
{
if (!currentValida tor.IsValid)
{
Label currentValidati onMessage = new Label();
currentValidati onMessage.Text = currentValidato r.ErrorMessage;
//Do something with the label (or whatever)
}
}
}

I used a variant of this to create my own display of the messages that
were to be displayed when validators failed. Hope it helps.

Have A Better One!

John M Deal, MCP
Necessity Software

Dan wrote:
hi newsgroup,

i need server side validation of user entries.
as i do have many fields, i would like to use reflection for checkinng
the IsValid property of all the Validators.
here is the reflection code

i don't understand why i get the following error:
Object does not match target type.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.Reflecti on.TargetExcept ion: Object does not
match target type.

at this line: object
ret=field.Field Type.GetPropert y("IsValid",typ eof(bool)).GetV alue(field,null );
the code is a loop the goes through all public fields of the user control:
public static bool checkValidators (Control page)
{
bool isValid=true;
FieldInfo[] fields=page.Get Type().GetField s();
foreach(FieldIn fo field in fields)
{

if(field.FieldT ype.IsSubclassO f(typeof(System .Web.UI.WebCont rols.BaseValida tor)))

{

object
ret=field.Field Type.GetPropert y("IsValid",typ eof(bool)).GetV alue(field,null );

isValid=Convert .ToBoolean(ret) ;
}
}
return isValid;
}

any ideas?
or maye there is another way to check the IsValid property of all the
validators in a fast way?
thanks for your help.
dan

Nov 18 '05 #3

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

Similar topics

2
10421
by: Doslil | last post by:
I am trying to validate the fields in my database.I have already validated the fields to check for not null.Here is what I have written for Numeric and text field. Private Function EENUM() On Error GoTo EENUMErr If IsNull(Me.EmployeesID) Or Me.EmployeesID = "" Or Me.EmployeesID < 0 Then MsgBox "Employee Number Cannot Be Left Blank", vbOKOnly, "Employee Number"
2
2691
by: Joey P | last post by:
Hi all, I am doing a project for university whereby i have to implement a simple database related to a frozen foods company. I am having some trouble though creating a validation rule for one of my fields. I have a table called "Product" and two of the fields included in this table are "Cost Price" and "Retail Price". I need to create a validation rule so that the Cost Price is always less than the Retail Price. I have tried...
3
2534
by: tony lock | last post by:
I am writing a simulation program, the main classes inherit from a base class which itself inherits from Control. I am using reflection to serialize these objects including the fields from control. This works perfectly apart from the field text in Control which always comes back null. Although the IDE always shows it having the correct value. Since the IDE presumably uses reflection to show the value, I am at a loss to know why this one...
6
21323
by: Paul | last post by:
I am trying to setup a field validator and tried using the control to validate set to a dropdown list box but did not seem to work. Is there anyway to set this up or do you need to use client side validation? Thanks -- Paul G Software engineer.
3
2715
by: Kivak Wolf | last post by:
Hi, Could someone give me a quick review of how to use the required field Validator? I'm completely at a loss, and MSDN does not help one bit. =/ Basically all I want done is to make sure that the user enters some text into a field called "txbName" and when they click on the submit button, if they have not entered anything into the field, a Required Field Validator will say "Invalid Name". Thanks,
10
5725
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the form the control is on? Basically I have a number of controls in a form that are required, and to check it I am setting the Validation Rule to "<>"IsNull" so that when the user tries to tab through/click out of a required area without entering...
10
9468
by: Rotsey | last post by:
Hi, I have a class that exist in my UI assembly that inherits from a class in a referenced assembly like this. class EmployeesDC { private string mstrEmployeeNumber; etc
4
1481
by: colin | last post by:
How can I access a field contained in fieldInfo by reference so I can pass it to a function ? ive tried __refvalue but this needs a type known at compile time, I can access it with SetValue and GetValue but in one case where the field is a structure I need to pass it as a ref to a function. as it is a structure i cant make a 'new' copy and pass that as a ref and then copy it back.
8
2739
by: Bryan | last post by:
I want my business objects to be able to do this: class Person(base): def __init__(self): self.name = None @base.validator def validate_name(self): if not self.name: return
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5418
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3701
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.