Click here to Skip to main content
15,885,244 members
Articles / Database Development / SQL Server

LINQ Challenges and SQL Server Compact Edition

Rate me:
Please Sign up or sign in to vote.
4.81/5 (40 votes)
25 Mar 2008CPOL22 min read 191.8K   1.1K   165  
Overcoming challenges with LINQ to SQL and using LINQ with SQL Server Compact Edition.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TimeApp.DataAccess;

namespace TimeApp.UI {

	public partial class ProjectListForm : Form {

		public ProjectListForm() {
			InitializeComponent();
		}

		private void ProjectListForm_Load(object sender, EventArgs e) {
			LoadProjects();
		}

		/// <summary>
		/// Updates the buttons based on the state of the interface.
		/// </summary>
		private void UpdateButtons() {
			btnEdit.Enabled = (lbProjects.SelectedItem != null);
			btnDelete.Enabled = btnEdit.Enabled;
		}

		/// <summary>
		/// Loads all projects into the interface.
		/// </summary>
		private void LoadProjects() {
			using (TimeAppDataContext DataContext = new TimeAppDataContext()) {
				lbProjects.DataSource = DataContext.GetAllProjects();
				lbProjects.DisplayMember = "Name";
				lbProjects.ValueMember = "ProjectId";
			}

			UpdateButtons();
		}

		/// <summary>
		/// Opens a project form for adding a new project.
		/// </summary>
		private void AddProject() {
			// Create a new project for adding.
			Project ProjectToAdd = new Project();

			// Show the project form.
			ProjectForm frmProject = new ProjectForm();
			if (frmProject.ShowDialog(this, ProjectToAdd, true) == DialogResult.OK) {
				// Save the new project.
				using (TimeAppDataContext DataContext = new TimeAppDataContext()) {
					DataContext.Projects.InsertOnSubmit(ProjectToAdd);
					DataContext.SubmitChanges();
				}

				// Reload the projects.
				LoadProjects();
			}
		}

		/// <summary>
		/// Opens a project form for editing the currently selected project.
		/// </summary>
		private void EditProject() {
			using (TimeAppDataContext DataContext = new TimeAppDataContext()) {
				// Get the project for editing.
				ProjectSummary SelectedProject = (ProjectSummary)lbProjects.SelectedItem;
				Project ProjectToEdit = DataContext.GetProject(SelectedProject.ProjectId);

				// Show the project form.
				ProjectForm frmProject = new ProjectForm();
				if (frmProject.ShowDialog(this, ProjectToEdit) == DialogResult.OK) {
					// Set tasks that are marked for deletion to be deleted from the DB.
					var MarkedTasks = from T in ProjectToEdit.Tasks
										where T.IsMarked
										select T;
					foreach (Task TaskToDelete in MarkedTasks)
						DataContext.Tasks.DeleteOnSubmit(TaskToDelete);

					// Save the edited project/tasks.
					DataContext.SubmitChanges();

					// Update the displayed project upon successful save.
					SelectedProject.Name = ProjectToEdit.Name;
					SelectedProject.DetectAway = ProjectToEdit.DetectAway;
				}
			}
		}

		/// <summary>
		/// Deletes the currently selected project.
		/// </summary>
		private void DeleteProject() {
			if (MessageBox.Show(this, "Delete this project?", "Delete Project", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
				// Delete the project.
				using (TimeAppDataContext DataContext = new TimeAppDataContext())
					DataContext.DeleteProject(((ProjectSummary)lbProjects.SelectedItem).ProjectId);

				// Reload the projects.
				LoadProjects();
			}
		}

		private void lbProjects_SelectedIndexChanged(object sender, EventArgs e) {
			UpdateButtons();
		}

		private void lbProjects_DoubleClick(object sender, EventArgs e) {
			EditProject();
		}

		private void btnAdd_Click(object sender, EventArgs e) {
			AddProject();
		}

		private void btnEdit_Click(object sender, EventArgs e) {
			EditProject();
		}

		private void btnDelete_Click(object sender, EventArgs e) {
			DeleteProject();
		}

	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I began programming on my Commodore 64 at around the age of 12. After migrating to DOS and then Windows, I decided to take on the Web. Several languages and platforms later, I have settled in with .NET nicely. I am currently the owner of a software consulting company and lead application developer for a learning-based technology consultation company.

The love of a finished application is usually at war with the desire to improve it as soon as it's released (they're never really finished).

Comments and Discussions