Alycus
Alycus

Reputation: 221

Trying to make validation prevent invalid input to database, not working

I'm trying to make a user registration form with the fields Name, Student ID, Year of Birth, Gender, Email Address and Password that is inputted into a Microsoft Access database. There is validation in place so that if, say, the password is less than 8 characters long, the user can't proceed unless it's changed.

But it's not working. The validation messages don't appear, except on the Email Address one which appears after tabbing away from the text field, and despite that invalid data can still be saved into the database. I cannot figure out why.

This is my HTML code:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h1 class="auto-style7">
        <strong>REGISTER</strong></h1>
    <p class="auto-style2">
        <strong><span class="auto-style3">
        <asp:Label ID="Label1" runat="server" Text="Name:"></asp:Label>
        </span></strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style5"></asp:TextBox>
        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please insert name" ForeColor="#CC3300" ValidationGroup="MemberGroup"></asp:RequiredFieldValidator>
        </strong></p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label2" runat="server" Text="Student ID:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox2" runat="server" CssClass="auto-style5"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Student ID must start with 'TP' followed by six digits." ForeColor="#CC3300" OnServerValidate="CustomValidator2_ServerValidate" ControlToValidate="TextBox2" ValidationGroup="MemberGroup" ValidateEmptyText="True"></asp:CustomValidator>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label3" runat="server" Text="Year of Birth:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:DropDownList ID="DropDownList2" runat="server" CssClass="auto-style5">
        </asp:DropDownList>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label4" runat="server" Text="Gender:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="auto-style5">
            <asp:ListItem Value="0"></asp:ListItem>
            <asp:ListItem>Female</asp:ListItem>
            <asp:ListItem>Male</asp:ListItem>
        </asp:DropDownList>
        <asp:CustomValidator ID="CustomValidator3" runat="server" ControlToValidate="DropDownList1" ErrorMessage="Please select a gender." ForeColor="#CC3300" OnServerValidate="CustomValidator3_ServerValidate" ValidationGroup="MemberGroup" ValidateEmptyText="True"></asp:CustomValidator>
        </strong>
    </p>
<p class="auto-style2">
        <strong>
        <asp:Label ID="Label5" runat="server" Text="Email address:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox4" runat="server" CssClass="auto-style5"></asp:TextBox>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Valid email address required." ForeColor="#CC3300" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="TextBox4" ValidationGroup="MemberGroup"></asp:RegularExpressionValidator>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label6" runat="server" Text="Password:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox5" TextMode="Password" runat="server" CssClass="auto-style5"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Password must be at least 8 characters long." ForeColor="#CC3300" OnServerValidate="CustomValidator1_ServerValidate" ControlToValidate="TextBox5" ValidationGroup="MemberGroup" ValidateEmptyText="True"></asp:CustomValidator>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label7" runat="server" Text="Confirm password:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox6" TextMode="Password" runat="server" CssClass="auto-style5"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox5" ControlToValidate="TextBox6" ErrorMessage="Confirm Password must match Password." ForeColor="#CC3300" ValidationGroup="MemberGroup"></asp:CompareValidator>
        </strong>
    </p>
<p class="auto-style3">
        &nbsp;</p>
<p>
        <strong>
        <asp:Button ID="Button1" runat="server" CssClass="auto-style3" OnClick="Button1_Click" Text="Register" />
        </strong>
    </p>
    <p class="auto-style3">
        &nbsp;</p>
</asp:Content>

And this is my C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace APUBadmintonSportsClubWebsite
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //append item to the collection
        for (int i = 1900; i <= 2007; i++)
            this.DropDownList2.Items.Add(i.ToString());
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //formulate a string containing the details of 
        //the database connection
        string connectionString = "provider=Microsoft.Ace.OleDB.12.0;"
            + "data source="
            + Page.Server.MapPath("App_Data\\ABC.accdb");

        //create a OIeDbConnection object to connect to the database
        System.Data.OleDb.OleDbConnection conn = new
        System.Data.OleDb.OleDbConnection(connectionString);

        //create a OleDbCommand object
        System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand();

        // add a collection of parameters associated with a OIeDbCommand

        cmd.Parameters.Add("StudentID", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["StudentID"].Value = this.TextBox2.Text;

        cmd.Parameters.Add("StudentName", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["StudentName"].Value = this.TextBox1.Text;

        cmd.Parameters.Add("YearBorn", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["YearBorn"].Value = this.DropDownList2.Text;

        cmd.Parameters.Add("Gender", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["Gender"].Value = this.DropDownList1.Text;

        cmd.Parameters.Add("StudentMail", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["StudentMail"].Value = this.TextBox4.Text;

        cmd.Parameters.Add("Password", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["Password"].Value = this.TextBox6.Text;

        // set the commandText property of the OleDbCommand object to
        // the INSERT statement
        cmd.CommandText = "INSERT INTO [Members] ([StudentID],[StudentName],[YearBorn],[Gender],[StudentMail],[Password]) VALUES (@StudentID,@StudentName,@YearBorn,@Gender,@StudentMail,@Password)";


        //open the database connection using the open() method of the OleDbConnection object
        conn.Open();

        //call the ExcuteNonQuery() method of the OleDbCommand object 
        //to run the INSERT statement
        int numberOfRows = cmd.ExecuteNonQuery(); //exception here when inserting duplicate data

        //close the OleDbConnection object using using the Close () method 
        conn.Close();

        if (Page.IsValid)
        {

        }

        //redirect to View.aspx
        Response.Redirect("Home.aspx");
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length < 8)
            args.IsValid = false;
        else
            args.IsValid = true;
    }

    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length < 8)
            args.IsValid = false;
        else
            args.IsValid = true;
    }

    protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (DropDownList1.SelectedValue == "0") //want to select text of drop down selection
            args.IsValid = false;
        else
            args.IsValid = true;
    }

}
}

Any help is appreciated, and I'll fill in more details if needed.

Upvotes: 0

Views: 775

Answers (1)

Brandon Spilove
Brandon Spilove

Reputation: 1569

You need to add ValidationGroup="MemberGroup" on your asp:Button control. Or you can just remove the ValidationGroup attribute from all the other validators. You also need to wrap the code in your Click event inside the if (Page.IsValid) check.

Upvotes: 2

Related Questions