Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Error while creating validation control in runtime

Error while creating validation control in runtime

Scheduled Pinned Locked Moved ASP.NET
designcsharpasp-netdotnetdata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lijukv
    wrote on last edited by
    #1

    I want to use Range Validator for 3 text boxes, I don't want to provide three Range Validator in the Web form(ie no design time Range validator). I don't want to use CustomValidation control(since I have to provide the validation as a function) ----------------------------- Error Happens in the "Validate()" method. The code and Error description is given below RangeValidator objRangeValidator = new RangeValidator(); objRangeValidator.ControlToValidate = "txtPreviousCostToCompany"; objRangeValidator.Type = ValidationDataType.Integer; objRangeValidator.MaximumValue = "10"; objRangeValidator.MinimumValue = "0"; objRangeValidator.ErrorMessage = "Error"; objRangeValidator.Validate(); Object reference not set to an instance of an object. 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. Source File: c:\inetpub\wwwroot\timesheetmanagement\recruitment.aspx.cs Line: 238 Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() System.Web.UI.WebControls.RangeValidator.ControlPropertiesValid() System.Web.UI.WebControls.BaseValidator.get_PropertiesValid() System.Web.UI.WebControls.BaseValidator.Validate() HRMG.Recruitment.recruitment.cmdSave_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\timesheetmanagement\recruitment.aspx.cs:238 System.Web.UI.WebControls.Button.onclick(EventArgs e) System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) System.Web.UI.Page.ProcessRequestMain() -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 236: objRangeValidator.MinimumValue = "0"; Line 237: objRangeValidator.ErrorMessage = "Error"; Line 238: objRangeValidator.Validate(); Line 239: Line 240: lblMessage.Text = ""; Thanks for reading this much

    M 1 Reply Last reply
    0
    • L lijukv

      I want to use Range Validator for 3 text boxes, I don't want to provide three Range Validator in the Web form(ie no design time Range validator). I don't want to use CustomValidation control(since I have to provide the validation as a function) ----------------------------- Error Happens in the "Validate()" method. The code and Error description is given below RangeValidator objRangeValidator = new RangeValidator(); objRangeValidator.ControlToValidate = "txtPreviousCostToCompany"; objRangeValidator.Type = ValidationDataType.Integer; objRangeValidator.MaximumValue = "10"; objRangeValidator.MinimumValue = "0"; objRangeValidator.ErrorMessage = "Error"; objRangeValidator.Validate(); Object reference not set to an instance of an object. 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. Source File: c:\inetpub\wwwroot\timesheetmanagement\recruitment.aspx.cs Line: 238 Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() System.Web.UI.WebControls.RangeValidator.ControlPropertiesValid() System.Web.UI.WebControls.BaseValidator.get_PropertiesValid() System.Web.UI.WebControls.BaseValidator.Validate() HRMG.Recruitment.recruitment.cmdSave_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\timesheetmanagement\recruitment.aspx.cs:238 System.Web.UI.WebControls.Button.onclick(EventArgs e) System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) System.Web.UI.Page.ProcessRequestMain() -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 236: objRangeValidator.MinimumValue = "0"; Line 237: objRangeValidator.ErrorMessage = "Error"; Line 238: objRangeValidator.Validate(); Line 239: Line 240: lblMessage.Text = ""; Thanks for reading this much

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, Because the RangeValidator is dynamically created at runtime and it's not added to a container, so when you call the Validate method, the error happens as the method uses the naming container to find the control which is specified in the ControlToValidate property in the control hierarchy. To work around this error, you simply add the validator to a container such as the Form element:

      RangeValidator objRangeValidator = new RangeValidator();
      this.FindControl("Form1").Controls.Add(objRangeValidator);
      ...

      For more information, you can see Control.NamingContainer Property[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups