Answered by:
Range validator's Validate() method throws a NullReferenceException.

Question
-
User-1623236949 posted
Fellow compadres,
Can some one explain why is this occurring? I declare the range validator at the class level like this: RangeValidator NumericRange = new RangeValidator();
Then in the on click method of the submit button I have the following:
NumericRange.ControlToValidate =
"txtInput";NumericRange.MinimumValue =
"10";NumericRange.MaximumValue =
"100";NumericRange.Type =
ValidationDataType.Integer;NumericRange.Validate();
if (!NumericRange.IsValid){
lblConfirmation.Text =
"Not A Number!";}
... and the thing frows the null exception. I do reference the WebControls and the HtmlControls, so please help me!
Thanks,
EJM
Tuesday, February 27, 2007 4:39 PM
Answers
-
User1806792307 posted
I'm sorry that overlooked the OnInit code. You should make sure the ControlToValidate, Minimum, and Maximum properties are all established before Page.Validate() fires. That happens after Page_Load but before your button's Click event handler runs.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 2, 2007 12:11 PM
All replies
-
User1335583151 posted
Hello my friend,
Which code line throws the exception? If it is the first, I would check that txtInput exists. If this is not the cause of the problem, submit your entire page to this question for further examination.
Kind regards
Scotty
Tuesday, February 27, 2007 5:16 PM -
User-1623236949 posted
Thanks Scotty for the reply. Here is the entire .cs page. This is test page where I was trying to make this work. Basically this is what's going on here:
I get a datareder stream from class file and build a radio button list. With each RB item I need to have 2 textboxes associated with, these are built at the runtime. User will be prompted to make chages to the text boxes. Since the input must be numeric I wanted to use a range validator. The code fails in the go_Click method on line that calls the NumericRange.Validate(); call.
The error is Null Reference Exception - it also states to use the "new" keyword to create an object instance.
I've got the hint form this link at MSDN http://msdn.microsoft.com/en-us/library/hxet6xwx(VS.80).aspx Ironically, all the forum postings that I went through not one had anything applicable to my situation.
using
System;using System.Data;
using
System.Data.SqlClient;using
System.Configuration;using
System.Collections;using
System.Text;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;using
test.bll;using
test.dal;public
partial class Default3 : System.Web.UI.Page{
string Sql = string.Empty; string Sql1 = string.Empty; TextBox txtReqInterval = new TextBox(); TextBox txtSuspInterval = new TextBox(); ArrayList alPriorities = new ArrayList(); ArrayList alReqInterval = new ArrayList(); ArrayList alSuspInterval = new ArrayList(); StringBuilder PrioritySQLString = new StringBuilder(); RangeValidator NumericRange = new RangeValidator(); protected void Page_Load(object sender, EventArgs e){
}
#region
Web Form Designer generated code override protected void OnInit(EventArgs e){
// Get Lookup Priority list SqlDataReader dr = new BusinessLogicLayer().bllGetPriorities();rblPriorities.Items.Clear();
while (dr.Read()){
ListItem lStatus = new ListItem();txtReqInterval =
new TextBox();txtSuspInterval =
new TextBox();txtReqInterval.Width = 30;
txtReqInterval.ID =
"RI" + Convert.ToString(dr.GetInt32(0));txtReqInterval.Text =
"30";txtReqInterval.MaxLength = 5;
txtSuspInterval.Width = 30;
txtSuspInterval.ID =
"SI" + Convert.ToString(dr.GetInt32(0));txtSuspInterval.Text =
"10";txtSuspInterval.MaxLength = 5;
lStatus.Text = dr.GetString(1).Trim();
lStatus.Value =
Convert.ToString(dr.GetInt32(0));pnlReqInt.Controls.Add(txtReqInterval);
pnlSuspInt.Controls.Add(txtSuspInterval);
pnlReqInt.Controls.Add(
new LiteralControl("<br>"));pnlSuspInt.Controls.Add(
new LiteralControl("<br>"));{
lStatus.Selected =
true;}
alPriorities.Add(
Convert.ToString(dr.GetInt32(0)) );rblPriorities.Items.Add(lStatus);
}
InitializeComponent();
base.OnInit(e);}
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent(){
}
#endregion
protected void go_Click(object sender, EventArgs e){
int id = 1; foreach (Control child in pnlReqInt.Controls){
if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")){
TextBox chk = (TextBox)child;chk.ID = id.ToString();
NumericRange.ControlToValidate =
"txtInput";NumericRange.MinimumValue =
"10";NumericRange.MaximumValue =
"100";NumericRange.Type =
ValidationDataType.Integer;NumericRange.Validate();
if (!NumericRange.IsValid){
lblConfirmation.Text =
"Not A Number!";}
else{
alReqInterval.Add(chk.Text);
}
}
id++;
}
foreach (Control child in pnlSuspInt.Controls){
if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")){
TextBox chk = (TextBox)child;alSuspInterval.Add(chk.Text);
}
}
Boolean done = false; Boolean stillnotdone = false; int i = 0; int j = 0; while (!done){
while (!stillnotdone){
lblReqInt.Text = alPriorities[j].ToString() +
", " + alReqInterval[j].ToString() + ", " + alSuspInterval[j].ToString() + ", ";j++;
break;}
i++;
if (i >= alPriorities.Count) done = true;}
lblConfirmation.Text = PrioritySQLString.ToString();
}
}
Wednesday, February 28, 2007 10:41 AM -
User1806792307 posted
Be sure that you are ADDING the control to the page before you call Validate(). The validator cannot report its error on the page if its not attached to the page. That's the most likely cause of this.
Since you are creating these controls programmatically:
- Make sure the ID property is assigned to unique values for each case.
- Assign the error message to the ErrorMessage property, not to some external label. The validator's job is to show the error.
- You really should create these controls each time the page is generated, not just on a postback. Treat them like any other control. Add them directly to the design view of the page, or programmatically in Page_Load. Then the button's OnClick event will automatically validate them (calls Page.Validate() for you). You only need to test Page.IsValid is true in your click event method.
Wednesday, February 28, 2007 2:29 PM -
User-1623236949 posted
As you can see from the code, I am adding the control in the OnInit method.
ID Properties are given unique ID consistring of 2-letter acronym and a record id from the database, such as "RI" + Convert.ToString(dr.GetInt32(0));.
I added the NumericRange.ErrorMessage="Not A Number"; just above where I call NumericRange.Validate() method.
Unfortunatelly that didn't help.
Any ideas? Thanks.
Friday, March 2, 2007 10:26 AM -
User1806792307 posted
I'm sorry that overlooked the OnInit code. You should make sure the ControlToValidate, Minimum, and Maximum properties are all established before Page.Validate() fires. That happens after Page_Load but before your button's Click event handler runs.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 2, 2007 12:11 PM