private void update_Click(object sender, EventArgs e) { if (databaseAvailable) { Entity en = new Entity(); List <User> userList = en.GetAllUsers(); dataGridView1.DataSource = userList; } else { dataGridView1.DataSource = XMLhelpers.FetchUsersFromXml(); } }
private void btn_deleteUSer_Click(object sender, EventArgs e) { if (databaseAvailable) { Entity en = new Entity(); en.DeleteUser(Convert.ToInt32(txt_deleteId.Text)); XMLhelpers.SaveUsersToXml(en.GetAllUsers()); } else { List <User> userList = XMLhelpers.FetchUsersFromXml(); User deletedUser = userList.FirstOrDefault(x => x.id == Convert.ToInt32(txt_deleteId.Text)); userList.Remove(deletedUser); XMLhelpers.SaveUsersToXml(userList); } }
private void createUserOk_Click(object sender, EventArgs e) { //If the database is available, save the data directly to the database if (databaseAvailable) { Entity en = new Entity(); if (en.CreateUser(txt_username.Text, txt_email.Text)) { MessageBox.Show("User succesfully created!"); XMLhelpers.SaveUsersToXml(en.GetAllUsers()); } else { MessageBox.Show("Failed to create user"); } //TODO add stuff! } else { //If the database is NOT available, save the data to the XML document. List <User> userList = XMLhelpers.FetchUsersFromXml(); int nextId = 0; foreach (var user in userList) { if (user.id > nextId) { nextId = user.id; } } nextId++; User newUser = new User(); newUser.email = txt_email.Text; newUser.username = txt_username.Text; newUser.id = nextId; userList.Add(newUser); XMLhelpers.SaveUsersToXml(userList); } }
private void btn_editUserOk_Click(object sender, EventArgs e) { if (databaseAvailable) { Entity en = new Entity(); en.EditUser(Convert.ToInt32(txt_editUserId.Text), txt_editUsername.Text, txt_editEmail.Text); XMLhelpers.SaveUsersToXml(en.GetAllUsers()); } else { List <User> userList = XMLhelpers.FetchUsersFromXml(); //Update the item in the list. User userToEdit = userList.FirstOrDefault(x => x.id == Convert.ToInt32(txt_editUserId.Text)); userToEdit.username = txt_editUsername.Text; userToEdit.email = txt_editEmail.Text; XMLhelpers.SaveUsersToXml(userList); } }
private void WindowsForms_Load(object sender, EventArgs e) { appPath = Path.GetDirectoryName(Application.ExecutablePath); scText = "Congratulations! You clicked the button! " + Environment.NewLine + "Now, enter some text and save it. It will be saved to a .txt file."; folderName = @"\UserFiles"; folderPath = appPath + folderName; scOpenFileDialog.InitialDirectory = folderPath; Entity en = new Entity(); databaseAvailable = en.IsDatabaseAvailable(); //If the database is available... if (databaseAvailable) { //Check if the users.xml file exists string usersXml = @"\users.xml"; var usersXmlExists = File.Exists(appPath + usersXml); //If it doesn't exist, and the database is available, create it with the values from the database if (!usersXmlExists) { XMLhelpers.SaveUsersToXml(en.GetAllUsers()); } //If the users in the database and the xml document differs, update the database with the local data. List <User> xmlUserList = XMLhelpers.FetchUsersFromXml(); List <User> userList = en.GetAllUsers(); bool inSync = true; //If the size of the lists are equal, we can loop through and compare directly if (xmlUserList.Count() == userList.Count()) { for (int i = 0; i < xmlUserList.Count(); i++) { if (xmlUserList[i].username != userList[i].username || xmlUserList[i].email != userList[i].email || xmlUserList[i].id != userList[i].id) { inSync = false; } } } else { //If the size is different, we dont need to loop to figure out they are de-synced. inSync = false; } if (!inSync) { MessageBox.Show("Detected unsynced data. Overwriting database with local data."); en.TruncateTable(); en.PopulateTable(xmlUserList); userList = en.GetAllUsers(); dataGridView1.DataSource = userList; } else { dataGridView1.DataSource = userList; } //The database is available, and data has been fetched from it. Now, just save the data into XML so it //is available offline also! XMLhelpers.SaveUsersToXml(userList); } else { this.Text += " - Offline mode"; MessageBox.Show("Can't connect to database. Starting in offline mode"); //Check if the users.xml file exists string usersXml = @"\users.xml"; var usersXmlExists = File.Exists(appPath + usersXml); //If it doesn't exist, and the database is not available, create it with a placeholder user if (!usersXmlExists) { List <User> placeholderUserList = new List <User>(); User placeholderUser = new User(); placeholderUser.id = 1; placeholderUser.username = "******"; placeholderUser.email = "PlaceholderEmail"; placeholderUserList.Add(placeholderUser); XMLhelpers.SaveUsersToXml(placeholderUserList); } //In offline mode, all data is loaded from the local xml document. dataGridView1.DataSource = XMLhelpers.FetchUsersFromXml(); } }