I am designing a windows form 3-tier application using C# and SQL. I have designed user registration page which is connected with the database using stored procedures. I have put textboxes to take input from user. I have used unique constraint for my user name column. Since I am using 3-tier application architecture so i have placed my register_user function in BLL and calling it from button in UI. When i typed repeated user name in text box it throws an exception(as expected) of unique constraint, but what i wish is that this message should be shown in a windows box and my application should not stop working. I have tried to use try catch in register_user function but it was of no use. Similarly I have also tried to use try catch in my UI(register button) but again failed. I am posting my code here: Thanks
//BLL
public void register_user(string First_Name,string Last_Name,string User_Name,string Password,string Date_Of_Birth,string Security_Question,string Security_Answer)
{
try
{
DAL obj = new DAL();
obj.OpenConnection();
obj.LoadSpParameters("UR", First_Name, Last_Name, User_Name, Password, Date_Of_Birth, Security_Question, Security_Answer);
obj.ExecuteQuery();
obj.UnLoadSpParameters();
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
DAL obj2 = new DAL();
obj2.CloseConnection();
}
}
// UI
private void register_button_Click(object sender, EventArgs e)
{
if (first_name_text.Text == "" || last_name_text.Text == "" || user_name_text.Text == "" || password_text.Text == "" || confirm_password_text.Text == "" || answer_txt.Text == "")
{
alert1_label.Show();
error1_label.Show();
error1_label.Text = "You cannot left mandatory fields empty";
}
if (password_text.Text.Length <= 8)
{
alert1_label.Show();
error1_label.Show();
error1_label.Text = "Password must be greater than 8 characters";
password_text.Clear();
confirm_password_text.Clear();
return;
}
if (first_name_text.Text.Any(Char.IsDigit) || first_name_text.Text.Any(Char.IsPunctuation) || first_name_text.Text.Any(Char.IsSeparator) || first_name_text.Text.Any(Char.IsSymbol))
{
alert1_label.Show();
error1_label.Show();
error1_label.Text = "Numbers and Special Characters are not allowed in first name";
first_name_text.Clear();
return;
}
if (last_name_text.Text.Any(Char.IsDigit) || last_name_text.Text.Any(Char.IsPunctuation) || last_name_text.Text.Any(Char.IsSeparator) || last_name_text.Text.Any(Char.IsSymbol))
{
alert1_label.Show();
error1_label.Show();
error1_label.Text = "Numbers and Special Characters are not allowed in last name";
last_name_text.Clear();
return;
}
if (!user_name_text.Text.Any(Char.IsLetter))
{
alert1_label.Show();
error1_label.Show();
error1_label.Text = "User Name must contain atleast one alphabet, and must be atleast 4 characters long.";
user_name_text.Clear();
return;
}
if (user_name_text.Text.Length <= 3)
{
alert1_label.Show();
error1_label.Show();
error1_label.Text = "User Name must contain atleast one alphabet, and must be atleast 4 characters long.";
user_name_text.Clear();
return;
}
else
{
try
{
error1_label.Hide();
alert1_label.Hide();
BLL obj = new BLL();
obj.register_user(first_name_text.Text, last_name_text.Text, user_name_text.Text, password_text.Text, date_of_birth_text.Text, security_question_text.SelectedItem.ToString(), answer_txt.Text);
MessageBox.Show("Registration Successful");
UP frm = new UP();
frm.Text = "Welcome" + " " + first_name_text.Text;
this.Dispose();
frm.Show();
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}