asp.net RequiredFieldValidator

4.4k views Asked by At

I have a problem. I have a registration and some RequiredFieldValidator controlls.

Problem:

If i leave the textbox empty, then i can see the errormessage. But it write the values in my database. i want that it stops, and not writing the values in my DB.

Thank you very much!

Kevin

Aspx

<tr>
    <td id="LabelBenutzername" class="auto-style2">Benutzername</td>
    <td>
        <asp:TextBox ID="TextBoxRBenutzername" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
            runat="server" ControlToValidate="TextBoxRBenutzername" 
            ErrorMessage="Bitte einen Benutzernamen eingeben" ForeColor="Red">
        </asp:RequiredFieldValidator>
    </td>
</tr>

Codebehind

if (IsPostBack)
        {

            SqlCommand cmd = new SqlCommand("select * from tabUser where Benutzername = @Benutzername", con);
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@Benutzername";
            param.Value = TextBoxRBenutzername.Text;
            cmd.Parameters.Add(param);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();


            if (reader.HasRows)
            {
                Label1.Text = "User Id already exists";
                con.Close();
                return;
            }

            con.Close();

        }

        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBFitnessBlogConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = con; //assigning connection to command
            cmd.CommandType = CommandType.Text; //representing type of command
            //cmd.CommandText = "INSERT INTO UserDetails (Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values
            // (@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)";
            cmd.CommandText = "INSERT INTO tabUser values(@Benutzername,@Passwort,@Vorname,@Nachname,@Email)";

            //adding parameters with value
            cmd.Parameters.AddWithValue("@Benutzername", TextBoxRBenutzername.Text.ToString());
            cmd.Parameters.AddWithValue("@Passwort", TextBoxRPasswort.Text.ToString());
            cmd.Parameters.AddWithValue("@Vorname", TextBoxRVorname.Text.ToString());
            cmd.Parameters.AddWithValue("@Nachname", TextBoxRNachname.Text.ToString());
            cmd.Parameters.AddWithValue("@Email", TextBoxREmail.Text.ToString());
            con.Open(); //opening connection
            cmd.ExecuteNonQuery();  //executing query
            con.Close(); //closing connection
            Label1.Text = "Registration erfolgreich..";

        }
        catch (Exception ex)
        {
            Label1.Text = "Registration erfolgreich NICHT..";
        }

    }
4

There are 4 answers

0
Humpy On

I don't see your submit button. But I have used your code to show you how I do mine. It looks like you are missing the ValidationGroup in both controls.

<tr>
   <td id="LabelBenutzername" class="auto-style2">Benutzername</td>
   <td>
       <asp:TextBox ID="TextBoxRBenutzername" runat="server"></asp:TextBox>
       <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBoxRBenutzername" ValidationGroup="Insert" ErrorMessage="Bitte einen Benutzernamen eingeben" ForeColor="Red"></asp:RequiredFieldValidator>
       <asp:Button ID="btnSubmit" runat="server" Text="Submit" Font-Size="Smaller" Height="29px" OnClick="btnSubmit_Click" Width="59px" ValidationGroup="Insert" CausesValidation="true" />
   </td>
</tr>

I hope this helps.

Note that you can also use a validation summary.

0
Scott Perry On

Quick answer: In your code-behind file, write into the event to check for validation prior to proceeding with the database update.

    protected void btnUpdateSettings_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            // Event Programming Code Goes Here
        }
    }

The idea would be that if any controls had triggered validation controls, then the form could post-back, but then there would be no code execution.

0
Tim On

Before you make your call to the database to update your data, check

Page.IsValid == true

before you make your update.

This should be false if your validation failed.

0
Karl Anderson On

In your code-behind, wrap the database logic in a check to see if the page is valid or not, like this:

if(Page.IsValid)
{
    // Do database logic here
}