Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
            }
Posted
Updated 2-Nov-15 6:16am
v3
Comments
Sinisa Hajnal 2-Nov-15 6:20am    
Check for minor differences between events that work and those that don't. Maybe you're missing something simple. Second, check each part of linq in btnSave, maybe something is different between messages.

If you didn't already, wrap your database calls in try..catch..finally and close the connection in finally block so you don't get memory leaks when database call fails.
ZurdoDev 2-Nov-15 7:28am    
Sounds like your regex statement is not working?
Member 12045962 2-Nov-15 7:33am    
No Regex works perfectly. but the error set doesn't show in message box if info is incorrect...
ZurdoDev 2-Nov-15 7:39am    
You need to debug this to see what is happening.
Member 12045962 2-Nov-15 7:40am    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900