473,327 Members | 2,055 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,327 software developers and data experts.

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.Reflection.TargetException: Object does not
match target type.

at this line: object
ret=field.FieldType.GetProperty("IsValid",typeof(b ool)).GetValue(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.GetType().GetFields();
foreach(FieldInfo field in fields)
{

if(field.FieldType.IsSubclassOf(typeof(System.Web. UI.WebControls.BaseValidator)))
{

object
ret=field.FieldType.GetProperty("IsValid",typeof(b ool)).GetValue(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 1457
> 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.Validators)
{
IValidator currentValidator = currentControl as IValidator;
if (currentValidator != null)
{
if (!currentValidator.IsValid)
{
Label currentValidationMessage = new Label();
currentValidationMessage.Text = currentValidator.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.Reflection.TargetException: Object does not
match target type.

at this line: object
ret=field.FieldType.GetProperty("IsValid",typeof(b ool)).GetValue(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.GetType().GetFields();
foreach(FieldInfo field in fields)
{

if(field.FieldType.IsSubclassOf(typeof(System.Web. UI.WebControls.BaseValidator)))

{

object
ret=field.FieldType.GetProperty("IsValid",typeof(b ool)).GetValue(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
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...
2
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...
3
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....
6
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...
3
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...
10
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...
10
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
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...
8
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
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...
1
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.