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..";
}
}
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.
I hope this helps.
Note that you can also use a validation summary.