private void confirmButtonClicked(object sender, RoutedEventArgs e) { string errors = checkAccount(); if (errors == "") { connection.Open(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_addUser"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@name", nameBox.Text); command.Parameters.AddWithValue("@username", usernameBox.Text); command.Parameters.AddWithValue("@password", passwordBox.Password); command.Parameters.AddWithValue("@email", emailBox.Text); command.Parameters.AddWithValue("@birthday", birthday.ToShortDateString()); command.ExecuteNonQuery(); connection.Close(); errorMessagesBox messageBox = new errorMessagesBox("account created"); messageBox.ShowDialog(); Close(); } else { errorMessagesBox messageBox = new errorMessagesBox(errors); messageBox.ShowDialog(); } }
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(); } }
//various click events void chatbutton_Click(object sender, RoutedEventArgs e)/////////////make the chat names so when they click it you can get the details of the chat/////////////////// { messages.Text = ""; connection.Open(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_getMessages"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@lastRecieved", lastRecieved); command.Parameters.AddWithValue("@groupID", groupID); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable table = new DataTable(); adapter.Fill(table); //MessageBox.Show(table.ToString()); //connection.Close(); Button clickedButton = sender as Button; //connection.Open(); command = new SqlCommand(); command.Connection = connection; command.CommandText = String.Format("SELECT * FROM [chat access] WHERE groupID = {0}", clickedButton.Tag); command.CommandType = CommandType.Text; adapter = new SqlDataAdapter(command); table = new DataTable(); adapter.Fill(table); List <string> peopleInGroup = new List <string>(); foreach (DataRow row in table.Rows) { int accountID = Convert.ToInt32(row["accountID"]); command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_getUsernameByID"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@accountID", accountID); SqlDataAdapter swagAdapter = new SqlDataAdapter(command); DataTable userTable = new DataTable(); swagAdapter.Fill(userTable); if (userTable.Rows.Count > 0) { peopleInGroup.Add(userTable.Rows[0]["username"].ToString()); } } SqlCommand commandtwo = new SqlCommand(); commandtwo.Connection = connection; commandtwo.CommandText = "usp_getChatGroup"; commandtwo.CommandType = CommandType.StoredProcedure; commandtwo.Parameters.AddWithValue("@name", chatname); adapter = new SqlDataAdapter(commandtwo); DataTable chatgrouptable = new DataTable(); adapter.Fill(chatgrouptable); groupID = (int)clickedButton.Tag; connection.Close(); string message = "DETAILS \n "; foreach (string name in peopleInGroup) { message += String.Format("{0} \n", name); } errorMessagesBox detailsBox = new errorMessagesBox(message); detailsBox.Show(); }
private void signInButton_Click(object sender, RoutedEventArgs e) { connection.Open(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_Login"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@username", usernameBox.Text.ToLower()); command.Parameters.AddWithValue("@password", passwordBox.Password); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { int id = int.Parse(table.Rows[0]["accountID"].ToString()); #region pull all friends SqlCommand getFriendsCommand = new SqlCommand(); getFriendsCommand.Connection = connection; getFriendsCommand.CommandText = "usp_getFriends"; getFriendsCommand.CommandType = CommandType.StoredProcedure; getFriendsCommand.Parameters.AddWithValue("@accountID", id); SqlDataAdapter getFriendsAdapter = new SqlDataAdapter(getFriendsCommand); DataTable friendsTable = new DataTable(); adapter.Fill(table); foreach (DataRow friendRow in friendsTable.Rows) { int friendID = Convert.ToInt32(friendRow["accountID"]); string friendName = friendRow["name"].ToString(); bool isOnline = Convert.ToBoolean(friendRow["isOnline"]); User user = new User(friendID, friendName, isOnline); if (!Global.Users.ContainsKey(friendID)) { Global.Users.Add(friendID, user); } } #endregion #region pull all groups for the current users. SqlCommand getGroupCommand = new SqlCommand(); getGroupCommand.Connection = connection; getGroupCommand.CommandText = "usp_getAllChatGroups"; getGroupCommand.CommandType = CommandType.StoredProcedure; getGroupCommand.Parameters.AddWithValue("@accountID", id); adapter = new SqlDataAdapter(getGroupCommand); SqlDataAdapter getGroupAdapter = new SqlDataAdapter(getGroupCommand); DataTable groupTable = new DataTable(); adapter.Fill(groupTable); foreach (DataRow groupRow in groupTable.Rows) { int groupID = Convert.ToInt32(groupRow["groupID"]); string groupName = groupRow["name"].ToString(); Group group = new Group(groupID, groupName); if (!Global.Groups.ContainsKey(groupID)) { Global.Groups.Add(groupID, group); } } #endregion foreach (Group group in Global.Groups.Values) { SqlCommand getUsersInGroupCommand = new SqlCommand(); getUsersInGroupCommand.Connection = connection; getUsersInGroupCommand.CommandText = "usp_getGroupUsers"; getUsersInGroupCommand.CommandType = CommandType.StoredProcedure; getUsersInGroupCommand.Parameters.AddWithValue("GroupID", group.GroupID); SqlDataAdapter getsUsersInGroupAdapter = new SqlDataAdapter(getUsersInGroupCommand); DataTable getsUsersInGroupTable = new DataTable(); getsUsersInGroupAdapter.Fill(getsUsersInGroupTable); foreach (DataRow row in getsUsersInGroupTable.Rows) { int userID = Convert.ToInt32(row["accountID"]); User user; if (Global.Users.ContainsKey(userID)) { user = Global.Users[userID]; } else { command = new SqlCommand(); command.Connection = connection; command.CommandText = "usp_getUsernameByID"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@accountID", userID); SqlDataAdapter getUsernameByIDadapter = new SqlDataAdapter(command); getUsernameByIDadapter.Fill(table); string name = table.Rows[0]["username"].ToString(); user = new User(userID, name, false); //add user to Global.Users } if (!group.ChatAccess.ContainsKey(userID)) { //add user to chat access. } } } chatroom chat = new chatroom(id); Hide(); chat.ShowDialog(); Close(); } else { errorMessagesBox messageBox = new errorMessagesBox("there was an error logging in"); messageBox.ShowDialog(); } connection.Close(); }