private void btnAdd_Click(object sender, EventArgs e) { try { if (IsValidData()) { //If the data is valid, then initialize fields to string or int and instanciate Customer object contactType = (txtContactType.Text.ToUpper()); try { DataTable dataTable = ContactDb.GetAllContactTypes(); // get list of all contact types var lst = new List <String>(); foreach (DataRow row in dataTable.Rows) // Check to see if the contact type entered already exists { if (row["ContactType"].ToString() == contactType) { MessageBox.Show("Contact Type " + contactType + " already exists, please re-enter."); txtContactType.Text = ""; txtContactType.Focus(); return; } } } catch { MessageBox.Show("Database Error, contact types are not available. " + "Contact administrator"); txtContactType.Text = ""; txtContactType.Focus(); } //Attempt to add new contact type to database dbUpdateSuccessful = ContactDb.AddContactType(contactType); if (dbUpdateSuccessful == 0) { txtContactType.Text = ""; txtContactType.Focus(); MessageBox.Show("Database exception, contact type has " + "not been added. Contact administrator."); } else { txtContactType.Text = ""; txtContactType.Focus(); MessageBox.Show("Contact Type Added."); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "/n" + ex.StackTrace, "Exception"); } }
//================ ON FORM LOAD EVENT ======================================================= private void frmAddContact_Load(object sender, EventArgs e) { try { //Loads contact types into the form when the form first opens DataTable dataTable = ContactDb.GetAllContactTypes(); System.Diagnostics.Debug.WriteLine(dataTable.Columns); //allows user input to list states that begin wtih the input cboContactType.AutoCompleteSource = AutoCompleteSource.ListItems; cboContactType.AutoCompleteMode = AutoCompleteMode.SuggestAppend; // fill state combo box with state names from database var lst = new List <String>(); foreach (DataRow row in dataTable.Rows) { lst.Add(row["ContactType"].ToString()); } cboContactType.Items.Clear(); cboContactType.DataSource = lst; //sets intial values of state combo box and state SP pararamter to "" so defalut // value is null if no state is selected by the user. cboContactType.Text = ""; } catch { MessageBox.Show("Database Error, contact types are not available. " + "Contact administrator"); } }
private void loadFormData() { try { var dataTable = ContactDb.GetAllContactTypes(); // GET ALL CONTACTS var lst = new List <String>(); foreach (DataRow row in dataTable.Rows) { lst.Add(row["ContactType"].ToString()); } lstContactType.DataSource = lst; } catch { MessageBox.Show("Database Error, contact types are not available. " + "Contact administrator"); } }