private void button1_Click(object sender, EventArgs e) { try { String _name = Name_Textbox.Text; int _number; if (!(int.TryParse(Number_Textbox.Text, out _number))) { // MessageBox.Show("Invalid Number"); label4.Visible = true; } else { PhoneDirectory phone = new PhoneDirectory(_name, _number); BusinessLayer phoneDetail = new BusinessLayer(); bool result=phoneDetail.ValidateAndAdd(phone); if (result == true) { dataGridView1.DataSource = phoneDetail.ViewPhone(phone); Number_Textbox.Clear(); Name_Textbox.Clear(); } else { MessageBox.Show("cannot be Added"); } } } catch(Exception e1) { MessageBox.Show(e1.Message); } }
public bool Add(PhoneDirectory directory) { try { using (SqlConnection sq = new SqlConnection("Server=localhost;Database=phone_directory;Trusted_Connection=true")) { sq.Open(); var cmd = new SqlCommand("Insert into phone_dir values(@name,@phone)"); cmd.Connection = sq; cmd.Parameters.AddWithValue("@name",directory.name); cmd.Parameters.AddWithValue("@phone",directory.number); cmd.ExecuteNonQuery(); return true; } } catch(Exception) { return false; } }
public DataTable ViewPhone(PhoneDirectory phoneDetails) { return repository.viewAll(); }
public bool ValidateAndAdd(PhoneDirectory phoneDetails) { bool _validateResult=ValidatePhone(); if (_validateResult==true) { return repository.Add(phoneDetails); } else return false; }