//this will add new contacts to the database public void addContacts(Contact c) { using (DbConnection connection = factory.CreateConnection()) { if (connection == null) { MessageBox.Show("Connection Error"); return; } connection.ConnectionString = connectionString; connection.Open(); DbCommand command = factory.CreateCommand(); if (command == null) { MessageBox.Show("Command Error"); return; } //MessageBox.Show(c.getFname()); command.Connection = connection; command.CommandText = "Insert into tblContacts (Id, FirstName, LastName, Email, StreetAddress, " + "Birthday, Notes) values ('" + c.getId() + "','" + c.getFname() + "','" + c.getLname() + "','" + c.getEmail() + "','" + c.getAddress() + "','" + c.getBirthday().Date + "','" + c.getNotes() + "');"; command.ExecuteNonQuery(); connection.Close(); } }
//this will update a contact in the database public void updateContacts(Contact c) { using (DbConnection connection = factory.CreateConnection()) { if (connection == null) { MessageBox.Show("Connection Error"); return; } connection.ConnectionString = connectionString; connection.Open(); DbCommand command = factory.CreateCommand(); if (command == null) { MessageBox.Show("Command Error"); return; } command.Connection = connection; command.CommandText = "UPDATE tblContacts" + " SET FirstName = '" + c.getFname() + "', LastName ='" + c.getLname() + "', Email ='" + c.getEmail() + "', StreetAddress = '" + c.getAddress() + "', Birthday='" + c.getBirthday().Date + "', Notes ='" + c.getNotes() + "' WHERE Id='" + c.getId() + "';"; command.ExecuteNonQuery(); connection.Close(); } }
//a different way to do insert queries public void insertContacts(Contact c) { connection.Open(); SqlCommand cmd = connection.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into tblContacts (Id, FirstName, LastName, Email, StreetAddress, " + "Birthday, Notes) values ('" + c.getId() + "','" + c.getFname() + "','" + c.getLname() + "','" + c.getEmail() + "','" + c.getAddress() + "','" + c.getBirthday().Date + "','" + c.getNotes() + "');"; cmd.ExecuteNonQuery(); connection.Close(); }
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { btnChange.Visible = true; btnDelete.Visible = true; try { c = cm.getContacts()[listView1.SelectedItems[0].Index]; txtFirst.Text = c.getFname(); txtLast.Text = c.getLname(); txtEmail.Text = c.getEmail(); txtAddress.Text = c.getAddress(); txtExtra.Text = c.getNotes(); dateBirth.Value = c.getBirthday(); } catch (ArgumentOutOfRangeException ex) { //this is just to prevent the program from crashing //listview issue unrelated to the code logic } btnAdd.Visible = false; }