private void deleteFriend_Click(object sender, RoutedEventArgs e) { customMessageBox deleteFriendBox = new customMessageBox("delete friend"); deleteFriendBox.ShowDialog(); string friendUsername = deleteFriendBox.typedMessage; yesnoMessageBox areyousure = new yesnoMessageBox("are you sure you want to delete this friend?"); areyousure.ShowDialog(); if (areyousure.yes == true) { connection.Open(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_getIDbyUsername"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@username", friendUsername); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable table = new DataTable(); adapter.Fill(table); connection.Close(); if (table.Rows.Count > 0) { try { connection.Open(); SqlCommand command2 = new SqlCommand(); command2.Connection = connection; command2.CommandText = "usp_deleteFriend"; command2.CommandType = CommandType.StoredProcedure; command2.Parameters.AddWithValue("@accountID", accountID); command2.Parameters.AddWithValue("@friendID", table.Rows[0]["accountId"]); command2.ExecuteNonQuery(); connection.Close(); } catch (SqlException ex) { errorMessagesBox error = new errorMessagesBox("there was an error deleting this friend"); error.Show(); } LoadFriends(); } else { errorMessagesBox error = new errorMessagesBox("you are not friends with this user"); error.Show(); } } else { areyousure.Close(); } }
private void addFriend_Click(object sender, RoutedEventArgs e) { customMessageBox addFriendBox = new customMessageBox("add friend"); addFriendBox.ShowDialog(); string friendUsername = addFriendBox.typedMessage; connection.Open(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_getIDbyUsername"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@username", friendUsername); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable table = new DataTable(); adapter.Fill(table); connection.Close(); if (table.Rows.Count > 0) { connection.Open(); try { SqlCommand command2 = new SqlCommand(); command2.Connection = connection; command2.CommandText = "usp_addFriend"; command2.CommandType = CommandType.StoredProcedure; command2.Parameters.AddWithValue("@accountID", accountID); command2.Parameters.AddWithValue("@friendID", table.Rows[0]["accountID"].ToString()); command2.ExecuteNonQuery(); } catch (SqlException ex) { errorMessagesBox error = new errorMessagesBox("there was an error adding this friend"); error.Show(); } connection.Close(); LoadFriends(); } else { customMessageBox command2 = new customMessageBox("username doesn't exist"); command2.ShowDialog(); } }
void friendButton_Click(object sender, RoutedEventArgs e) { yesnoMessageBox messageBox = new yesnoMessageBox("would you like to create a chat with this person?"); messageBox.ShowDialog(); if (messageBox.yes == true) { customMessageBox newform = new customMessageBox("what would you like to name this group?"); newform.ShowDialog(); string groupname = newform.typedMessage; customMessageBox form = new customMessageBox("what would you like the password to be?"); form.ShowDialog(); string password = form.typedMessage; //creating chat connection.Open(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_createChatGroup"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@name", groupname); command.Parameters.AddWithValue("@password", password); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable table = new DataTable(); adapter.Fill(table); //adding self to chat int newGroupID = int.Parse(table.Rows[0]["groupID"].ToString()); command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_addGroupAccess"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@accountID", accountID); command.Parameters.AddWithValue("@groupID", newGroupID); command.ExecuteNonQuery(); //get id of the other person DataTable resultTable = new DataTable(); string username = ((Button)sender).Content.ToString(); command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_getIDbyUsername"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@username", username); SqlDataAdapter adapter2 = new SqlDataAdapter(command); adapter2.Fill(resultTable); int otherPersonID = Convert.ToInt32(resultTable.Rows[0]["accountID"]); //add other person to chat command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_addGroupAccess"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@accountID", otherPersonID); command.Parameters.AddWithValue("@groupID", newGroupID); command.ExecuteNonQuery(); connection.Close(); LoadChats(); } }