locked
To validate or not to validate RRS feed

  • Question

  • User-986098262 posted
    Hey people! Here is my situation - I've created a dynamic data website with one table. The fields of this table are mostly varchar() not NULL, and all fields are defined with default value. When trying to insert new row with empty values I get required validation on all fields. What would be the best way to enable this validation only on some of these fields, while using default values for the others? For example empty string for varchar or 0 for int, etc Thanks Michael
    Tuesday, December 16, 2008 9:00 AM

All replies

  • User444460997 posted

    no in this case it will be add

     what error its return when u add.

    Tuesday, December 16, 2008 9:10 AM
  • User-211498538 posted

    Hi,

    Yes you need to validate all your textboxes etc, unless you have told the database to accept "NULL's". The insert will not product an error then. 

    Its a security feature as well as looking better.

    This link explains how to validate your page.

    http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=46

    Tuesday, December 16, 2008 9:11 AM
  • User-986098262 posted
    Ok. In other words what I want is to remove required validation from some of the fields and substitute the null with another default value before it is actually inserted/updated in the database. What is the right way to accomplish this with dynamic data?
    Tuesday, December 16, 2008 9:38 AM
  • User-211498538 posted

    Yes,

    Modify your table and check the Allow Nulls box for the field you wish not to validate.

    The ID which should be the first field in the table should NOT allows NULL's always.

    If you go to 'Column Properties' which is at the bottom of SQL Server 2005. You can set a 'Default Value or Binding'. For example if you type in getdate(), it will automatically insert todays date.

    You could also default it to 1 or 0. Good if you are using checkboxes. You would need to make it a TinyInt if using checkboxes.

    Any other questions?

    Tuesday, December 16, 2008 9:53 AM
  • User-986098262 posted
    What is the right way to accomplish what I've asked without modifying the table?
    Tuesday, December 16, 2008 10:00 AM
  • User-211498538 posted

    The best way is to modify the tables. I strongly recommend doing it that way.

    It would be sloppy programming any other way if you dont use validation and less secure.

    Cheers.

    Tuesday, December 16, 2008 10:07 AM
  • User-986098262 posted
    Well, in my case modifying the table is not an option. I am pretty sure that what I'm asking is possible by extending the table class or changing the field templates..
    Tuesday, December 16, 2008 11:06 AM
  • User-211498538 posted

    If you just want to validate a textbox why dont you just use for example,

    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Display="dynamic"

    ErrorMessage="Enter Org Name" ControlToValidate="sOrgName"></asp:RequiredFieldValidator>

    ?

     

    Tuesday, December 16, 2008 11:12 AM
  • User-986098262 posted
    I'm sorry, you've got me wrong. What I'm looking for is actually an elegant way to disable the Required validation on some fields. They all are set to NOT NULL in the database and changing this is not an option for me. Thanks.
    Tuesday, December 16, 2008 11:48 AM
  • User-211498538 posted

    My final suggestion. If this doesnt help. Try reposting this again so its brand new. People will respond faster to brand new posts.

    http://www.aspdev.org/articles/asp.net-server-controls-disable-validation/

    Tuesday, December 16, 2008 11:57 AM
  • User-1005219520 posted

    It would be a mistake to change the schema to allow nulls - you don't want to do that. For some fields you want the DB to supply the value (via defaults from the DB). What you really want is allow NULL at the client (ASP.Net side) but not allow nulls on the server (the server will supply the value).

    The easiest way to do this is with scaffoldColumn(false) to hide those columns. The problem with that approach is you never see the hidden fields - even on List and details pages where you are just reading them. You can use Steve's approach to hide the fields on insert. Look for hiding columns at http://csharpbits.notaclue.net 

    Tuesday, December 16, 2008 12:34 PM