Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//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())
            {
                //Do registration here
               
                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");
                    //e.Cancel = true;
                    txtRegUserN.Clear();
                    //e.Cancel = true;
                    break;
                }
                else
                {
                   //errorProvRi.Icon = Properties.Resources.c;
                    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();
                //e.Cancel = true;
            }
            else
            {

                //errorProvRi.Icon = Properties.Resources.c;
                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();
                //e.Cancel = true;
            }
            else
            {
                //this.errorProvRi.Icon = Properties.Resources.c;
                errorProv.Dispose();
             
            }
        }

        private void txtRegEmail_Validating(object sender, CancelEventArgs e)
        {

            //Regex r = new Regex("^[a-zA-Z0-9){1,20}@[a-zA-Z0-9){1,20}.[a-zA-Z]{2,3}$");

            //if (!r.IsMatch(txtRegEmail.Text))
            //{
            //    this.errorProvRi.Icon = Properties.Resources.w;
            //    errorProv.SetError(txtRegEmail, "incorrect formate");
            //    e.Cancel = true;

            //}

            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();
                    //e.Cancel = true;
                    break;
                }
                else
                {
                    //this.errorProvRi.Icon = Properties.Resources.c;
                    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?
Posted
Updated 9-Oct-15 1:53am
v4
Comments
Ankur_Garg 9-Oct-15 7:49am    
Why would you want to show the message box again and again? Keep it outside the loop after closing the connection.
Member 12045962 9-Oct-15 7:51am    
No I want to show when user is registered... Like 'You have been registered'.

1 solution

It probably doesn't show you anything because the stored procedure isn't returning any rows: hence the reader is empty.

Have a look at your SP, and see exactly what it does. Unless it does a SELECT, the reader will not contain any data. It may be setting a return value instead, in which case you may need ExecuteScalar instead of ExecuteReader.
 
Share this answer
 
Comments
Ankur_Garg 9-Oct-15 8:36am    
Also if there would have been multiple rows returned, it would show multiple message box.
Member 12045962 9-Oct-15 8:57am    
I dropped the if (dr.HasRows == true), it might even fix the problem.
Member 12045962 9-Oct-15 9:06am    
//stored proc for registration form:

ALTER proc [dbo].[RegistrationForm]

(
@UserName nvarchar(50),
@Password varchar(50),
@Confirm varchar(50),
@Email nvarchar( 50)

)
as
insert into tblRegF ( UserName, Password, Confirm, Email)
values (@UserName,@Password ,@Confirm ,@Email )
OriginalGriff 9-Oct-15 10:08am    
That SP doesn't return any rows.
It will return a scalar value which represents the number of rows inserted though. Try ExecuteScalar instead.
But...don't store passwords in clear text!
See here:
http://www.commitstrip.com/wp-content/uploads/2013/09/Strips-Erreur-au-pilori-001-650-finalenglish.jpg
For how the badly it is considered.

And then see here:
http://www.codeproject.com/Tips/186585/Password-Storage-How-to-do-it
For how to fix it.
Member 12045962 9-Oct-15 10:20am    
I have resolved my problem by removing these lines... And now, it's showing me the msg...

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{

}

}

WEll, Thanks ton!!!

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