leonsas
leonsas

Reputation: 4908

Validation against Database type ASP.NET

I have a few fields in a webforms and currently I'm not implementing any form of validation but I intend to do so.

Suppose I have the following UpdateParameters in my SQLDataSource:

<UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Date" Type="DateTime" />
            <asp:Parameter Name="active" Type="Boolean" />
            <asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>

And a textbox for each of the fields. I tried, but couldn't find a built-in way to validate the TextBoxes against the parameter type, so for example, the Date_TextBox would validate that the input is in a DateTime format. I know I can do custom validators with regexes and whatnot, but I feel there's a built-in, simple way of doing it. Any ideas?

Upvotes: 1

Views: 260

Answers (1)

Jupaol
Jupaol

Reputation: 21365

Have you tried using the CompareValidator??

You can specify the type like this:

<asp:TextBox runat="server" ID="txt" />
<asp:CompareValidator runat="server" ErrorMessage="The type is not a valid integer" 
    ControlToValidate="txt"
    Operator="DataTypeCheck"
    Type="Integer"
    />
<asp:Button Text="Test valdiation" runat="server" />

The available types are:

  • String. Specifies a string data type.
  • Integer. Specifies a 32-bit signed integer data type.
  • Double. Specifies a double-precision floating-point number data type.
  • Date. Specifies a date data type.
  • Currency. Specifies a monetary data type.

Upvotes: 1

Related Questions