Update Name, Password and Email.
I have created event on the text boxes. If I put the incorrect information in the text boxes and click on Registration button then it doesn't show me the validation errors in message box as in the else code to join list of errors.
C# Code on btnSave:
private void metroButton1_Click(object sender, EventArgs e)
{
if (this.ValidateChildren())
{
string c = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("UpdateNameNPas", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar, 200).Value = this.metroTextBox3.Text;
cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 200).Value = this.metroTextBox2.Text;
cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 200).Value = this.metroTextBox4.Text;
int n = cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
MetroMessageBox.Show(this, "Setting has been updated");
}
else
{
var listOfErrors = this.errorProUpd.ContainerControl.Controls
.Cast<Control>()
.Select(c => this.errorProUpd.GetError(c))
.Where(s => !string.IsNullOrEmpty(s))
.ToList();
MetroMessageBox.Show(this, "Please correct errors:\n - " +
string.Join("\n - ", listOfErrors.ToArray()),
"Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
C# code on btnEmail: (the problem is here it doesn't join the error of regex in messagebox)
Regex r = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
if (!r.IsMatch(metroTextBox4.Text))
{
this.errorProUpd.SetError(this.metroTextBox4, "Incorrect format. E.g: UmmeAlqama@gmail.com");
e.Cancel = true;
}
else
{
string c = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("EmailSetting", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Email", this.metroTextBox4.Text);
int n = cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
this.errorProUpd.SetError(this.metroTextBox4, "Email already exist");
e.Cancel = true;
metroTextBox4.Clear();
}