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

NullReferenceException on Validate()

I have a custom composite control which has a validator for a textbox. The validator and textbox are declared in the class and created in the CreateChildControls() method

Here is the code for the textbox, in CreateChildControls()

searchBox = new TextBox();
searchBox.ID = "searchBox";
searchBox.EnableViewState = false;

It is then added to a TableCell, which is added to a row, etc.
Here is the validator being created (also in CreateChildControls()):

searchBoxValidator = new RequiredFieldValidator();
searchBoxValidator.ID = "searchBoxValidator";
searchBoxValidator.EnableViewState = false;
searchBoxValidator.ControlToValidate = "searchBox";
searchBoxValidator.ErrorMessage = "You must enter a product ID";

This validator, again, is added to a TableCell, etc.

On a submit button's click event, I call searchBoxValidator.Validate(), which throws a NullReferenceException:

[NullReferenceException: Object reference not set to an instance of an object.]
AdminToolProduct.ProductSelectControl.searchButton Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\admintoolproduct_1\controls\pro ductselectcontrol.cs:235
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
....
Any ideas? I've tried setting the ControlToValidate property of the validator to
UniqueID+"_searchBox", resulting in the same error. Besides, if the control to validate is not found, I thought an HttpException was thrown. I also noticed that for some reason, no client side validation is done, even though I explicity tried setting the EnableClientScript to true (doesn't seem to be anything showing up in the source). Am I setting up this validator in the wrong place, or doing something else wrong? Thank you for your time.
Nov 18 '05 #1
5 2612
"Jeff Evans" <Je*******@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...
I have a custom composite control which has a validator for a textbox. The validator and textbox are declared in the class and created in the
CreateChildControls() method
Here is the code for the textbox, in CreateChildControls()

searchBox = new TextBox();
searchBox.ID = "searchBox";
searchBox.EnableViewState = false;

It is then added to a TableCell, which is added to a row, etc.
Here is the validator being created (also in CreateChildControls()):

searchBoxValidator = new RequiredFieldValidator();
searchBoxValidator.ID = "searchBoxValidator";
searchBoxValidator.EnableViewState = false;
searchBoxValidator.ControlToValidate = "searchBox";
searchBoxValidator.ErrorMessage = "You must enter a product ID";

This validator, again, is added to a TableCell, etc.

On a submit button's click event, I call searchBoxValidator.Validate(), which throws a NullReferenceException:
[NullReferenceException: Object reference not set to an instance of an object.] AdminToolProduct.ProductSelectControl.searchButton Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\admintoolproduct_1\controls\pro ductselectcontrol.cs:235 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
...
Any ideas? I've tried setting the ControlToValidate property of the validator to UniqueID+"_searchBox", resulting in the same error. Besides, if the

control to validate is not found, I thought an HttpException was thrown. I
also noticed that for some reason, no client side validation is done, even
though I explicity tried setting the EnableClientScript to true (doesn't
seem to be anything showing up in the source). Am I setting up this
validator in the wrong place, or doing something else wrong? Thank you for
your time.
------------

Perhaps you need to call EnsureChildControls in the Click event before
referencing other controls.
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #2

"John Saunders" wrote:
Perhaps you need to call EnsureChildControls in the Click event before
referencing other controls.


Thanks for the reply, but I have already called EnsureChildControls() in OnInit() (and I did try adding it in the click event just to be sure). I don't understand the NullReferenceException here, since the Validator itself is definitely instantiated at that point. The last function mentioned in the stack trace is:

System.Web.UI.WebControls.BaseValidator.CheckContr olValidationProperty(String name, String propertyName)

but the docs for it only mention throwing HttpException... I thought I had listed the stack trace before, but it looks like I didn't:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls.BaseValidator.CheckContr olValidationProperty(String name, String propertyName)
System.Web.UI.WebControls.BaseValidator.ControlPro pertiesValid()
System.Web.UI.WebControls.BaseValidator.get_Proper tiesValid()
System.Web.UI.WebControls.BaseValidator.Validate()
Any other suggestions? It's not that big of a deal, since one can accomplish the same thing as validators with a little extra code, but it still *should* work. Thanks.
Nov 18 '05 #3

"John Saunders" wrote:
Perhaps you need to call EnsureChildControls in the Click event before
referencing other controls.


Thanks for the reply, but I have already called EnsureChildControls() in OnInit() (and I did try adding it in the click event just to be sure). I don't understand the NullReferenceException here, since the Validator itself is definitely instantiated at that point. The last function mentioned in the stack trace is:

System.Web.UI.WebControls.BaseValidator.CheckContr olValidationProperty(String name, String propertyName)

but the docs for it only mention throwing HttpException... I thought I had listed the stack trace before, but it looks like I didn't:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls.BaseValidator.CheckContr olValidationProperty(String name, String propertyName)
System.Web.UI.WebControls.BaseValidator.ControlPro pertiesValid()
System.Web.UI.WebControls.BaseValidator.get_Proper tiesValid()
System.Web.UI.WebControls.BaseValidator.Validate()
Any other suggestions? It's not that big of a deal, since one can accomplish the same thing as validators with a little extra code, but it still *should* work. Thanks.
Nov 18 '05 #4
"Jeff Evans" <Je*******@discussions.microsoft.com> wrote in message
news:10**********************************@microsof t.com...

"John Saunders" wrote:
Perhaps you need to call EnsureChildControls in the Click event before
referencing other controls.
Thanks for the reply, but I have already called EnsureChildControls() in

OnInit() (and I did try adding it in the click event just to be sure). I
don't understand the NullReferenceException here, since the Validator itself
is definitely instantiated at that point. The last function mentioned in
the stack trace is:
System.Web.UI.WebControls.BaseValidator.CheckContr olValidationProperty(Strin
g name, String propertyName)
but the docs for it only mention throwing HttpException... I thought I had listed the stack trace before, but it looks like I didn't:

[NullReferenceException: Object reference not set to an instance of an object.] System.Web.UI.WebControls.BaseValidator.CheckContr olValidationProperty(Strin
g name, String propertyName) System.Web.UI.WebControls.BaseValidator.ControlPro pertiesValid()
System.Web.UI.WebControls.BaseValidator.get_Proper tiesValid()
System.Web.UI.WebControls.BaseValidator.Validate()
Any other suggestions? It's not that big of a deal, since one can

accomplish the same thing as validators with a little extra code, but it
still *should* work. Thanks.

One area to think about is, why do you need to manually call Validate? Why
isn't the page validation framework calling it? I ask because it may be that
you're calling Validate too early. If the page validation framework were to
call Validate for you, one would presume it would be done at the proper
time.
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #5

"John Saunders" wrote:
One area to think about is, why do you need to manually call Validate? Why
isn't the page validation framework calling it? I ask because it may be that
you're calling Validate too early. If the page validation framework were to
call Validate for you, one would presume it would be done at the proper
time.


I don't want all the validators to act at the Page level, as is discussed at:

http://www.peterblum.com/VAM/ValMain...nUrl=Home.aspx

I don't understand how it could be too late. This is happening during the postback events, after Init and Load have completed for the composite and all the children. The only thing later is PreRender, save viewstate, and render.

I'll have to do some experimentation. Thanks for the reply.
Nov 18 '05 #6

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

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
5
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
2
by: Enrico Pangan | last post by:
I'm trying to call some functions in a C++ Dll, "Library.dll" from C#. Some functions work but some return the NullReferenceException. I have here the source code for the C++ version and for the...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
9
by: Xero | last post by:
could anybody describes a scenario where this error will occur? i have visited the page about the NullReferenceException in msdn library but i still have no idea what it is about. thanks. ...
0
by: statlerw | last post by:
I have successfully implemented drag and drop in my application to allow the reordering of columns by dragging and dropping them in the same datagridvire (Net 2.0). If i take it relatively...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
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,...

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.