Address Book – .NET Database Access Intro

Addresses AddressesInterface

AddressesDataSet

This is my first stab at using C# .NET to access a simple database. The database is a simple Microsoft Access database. However this has showed me what you can do when a program can store information in a database. Really cool stuff!!! I’ve included 3 screenshots this time. Along with the Class Designer and Interface screenshots, there’s an overview if the dataset tables. Each dataset is doing simple SELECT and FROM queries from the main database. I’m also using the default binding navigator to scroll thru, delete, and save the changed data. And I also enabled a drag and drop feature on the picture box to upload a picture. Also if you laugh at the sample employees and where they come from kudos. You share my sense of humor. Now onto the code. Or you can download the source for yourself.

Cheers
Tails8


MainForm Class


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;

namespace Addresses
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            //this.photographPictureBox.AllowDrop = true;
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            //Lambda expression for the exit menu item.
            exitToolStripMenuItem.Click += (from, ea) => this.Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'myDatabaseDataSet.Addresses' table. You can move, or remove it, as needed.
            this.addressesTableAdapter.Fill(this.myDatabaseDataSet.Addresses);
            this.photographPictureBox.AllowDrop = true;     //This is to enable the ability to drag and drop in the picturebox.

        }

        private void addressesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.addressesBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.myDatabaseDataSet);

        }

        private void photographPictureBox_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (filePaths.Length > 0)
                {
                    // Attempt to load, may not be valid image
                    string path = filePaths[0];
                    try
                    {
                        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(path)))
                        {
                            Image tempImage = Image.FromStream(memoryStream);
                            photographPictureBox.Image = new Bitmap(tempImage);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error loading file: " + ex.Message);
                    }
                }
            }
        }

        private void photographPictureBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }
    }
}

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s