//Validating event code on text box:
private void Reg_Load(object sender, EventArgs e)
{
this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;
}
private void btnRegistration_Click_1(object sender, EventArgs e)
{
if (this.ValidateChildren())
{
string c = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("RegistrationForm", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", txtRegUserN.Text);
cmd.Parameters.AddWithValue("@Password", txtRegPass.Text);
cmd.Parameters.AddWithValue("@Confirm", txtRegPassConfirm.Text);
cmd.Parameters.AddWithValue("@Email", txtRegEmail.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
MessageBox.Show("Data Inserted Succesfully");
}
con.Close();
}
else
{
var listOfErrors = this.errorProv.ContainerControl.Controls
.Cast<Control>()
.Select(c => this.errorProv.GetError(c))
.Where(s => !string.IsNullOrEmpty(s))
.ToList();
MessageBox.Show("please correct validation errors:\n - " +
string.Join("\n - ", listOfErrors.ToArray()),
"Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void txtRegUserN_Validating(object sender, CancelEventArgs e)
{
string c = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("UserNameReg", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", this.txtRegUserN.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows == true)
{
errorProv.SetError(txtRegUserN, "UserName = " + dr[1].ToString() + " Already exist");
txtRegUserN.Clear();
break;
}
else
{
errorProv.Dispose();
}
}
}
private void txtRegPass_Validating(object sender, CancelEventArgs e)
{
if (this.txtRegPass.TextLength < 8)
{
this.errorProvRi.Icon = Properties.Resources.w;
this.errorProv.SetError(this.txtRegPass, "Password must be at least 8 character");
txtRegPass.Clear();
}
else
{
errorProv.Dispose();
}
}
private void txtRegPassConfirm_Validating(object sender, CancelEventArgs e)
{
if (this.txtRegPassConfirm.Text != this.txtRegPass.Text)
{
this.errorProvRi.Icon = Properties.Resources.w;
this.errorProv.SetError(this.txtRegPassConfirm, "Password and Confirm must be the same");
txtRegPassConfirm.Clear();
}
else
{
errorProv.Dispose();
}
}
private void txtRegEmail_Validating(object sender, CancelEventArgs e)
{
string c = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("EmailReg", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Email", this.txtRegEmail.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows == true)
{
errorProv.SetError(txtRegEmail, "Email = " + dr[4].ToString() + "is Already exist");
txtRegEmail.Clear();
break;
}
else
{
errorProv.Dispose();
}
}
}
}
}
I've made the registration form with text box: User Name, Password, Confirm and Email. I used the validating event on my text box. Application is working fine but, when I enter the correct information in the text box and press registration button so, it saves data in database but doesn't show me the message box. Please, tell me why it doesn't show the message box?