Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / XML

NoteBook using XML and ADO.NET

Rate me:
Please Sign up or sign in to vote.
3.67/5 (15 votes)
22 Dec 20024 min read 102.3K   2K   45  
An article on using XML and ADO.NET to create an appointment/address book.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace NoteBook
{
	/// <remarks>
	/// This class is to : 
	///		1- Add new Address to the database.
	///		2- Edit an Existing Address.
	///	
	///	This class create an object of NBDatabase class.
	/// </remarks>
	public class Address : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.TextBox textBox7;
		private System.Windows.Forms.TextBox textBox8;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.StatusBar statusBar1;
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		
		/// <summary>
		/// Variable to hold the data row view index.
		/// it will help in updating 
		/// </summary>
		private int updateRowIndex;

		/// <summary>
		/// An object to handle the database access.
		/// Sending to the constructer the path to the xml and xsd files
		/// </summary>
		NBDatabase objDatabase = new NBDatabase("..\\..\\AllTables.xml","..\\..\\AddressBookTables.xsd");

        /// <summary>
		/// Constructor. It hides the Update Button Becaues this form
		/// is by default for adding new Address.
        /// </summary>
		public Address()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// hiding the update button
            button3.Hide();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.label9 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox8 = new System.Windows.Forms.TextBox();
			this.textBox7 = new System.Windows.Forms.TextBox();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.statusBar1,
																					this.label9,
																					this.button2,
																					this.label8,
																					this.label7,
																					this.label6,
																					this.label5,
																					this.label4,
																					this.label3,
																					this.label2,
																					this.label1,
																					this.textBox8,
																					this.textBox7,
																					this.textBox6,
																					this.textBox5,
																					this.textBox4,
																					this.textBox3,
																					this.textBox2,
																					this.textBox1,
																					this.button1,
																					this.button3});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(592, 384);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = " Address Book";
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(3, 359);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(586, 22);
			this.statusBar1.TabIndex = 18;
			this.statusBar1.Text = "Fill form and click on Add Address or click on Close to exit";
			// 
			// label9
			// 
			this.label9.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label9.Location = new System.Drawing.Point(8, 304);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(536, 16);
			this.label9.TabIndex = 17;
			this.label9.Text = "* for  more than one entry separate them with \" ; \". ex. FirstEntry ; SecondEntry" +
				" ...";
			// 
			// button2
			// 
			this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.button2.Location = new System.Drawing.Point(360, 328);
			this.button2.Name = "button2";
			this.button2.TabIndex = 10;
			this.button2.Text = "Close";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label8
			// 
			this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label8.Location = new System.Drawing.Point(336, 200);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(40, 23);
			this.label8.TabIndex = 15;
			this.label8.Text = "Note";
			// 
			// label7
			// 
			this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label7.Location = new System.Drawing.Point(8, 192);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(80, 23);
			this.label7.TabIndex = 14;
			this.label7.Text = "Mail Address";
			// 
			// label6
			// 
			this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label6.Location = new System.Drawing.Point(320, 128);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(48, 23);
			this.label6.TabIndex = 13;
			this.label6.Text = "E-mail *";
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label5.Location = new System.Drawing.Point(8, 128);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 23);
			this.label5.TabIndex = 12;
			this.label5.Text = "Work Phone *";
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label4.Location = new System.Drawing.Point(288, 64);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(88, 23);
			this.label4.TabIndex = 11;
			this.label4.Text = "Home Phone *";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label3.Location = new System.Drawing.Point(8, 64);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 23);
			this.label3.TabIndex = 10;
			this.label3.Text = "Mobile #. *";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label2.Location = new System.Drawing.Point(288, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 23);
			this.label2.TabIndex = 9;
			this.label2.Text = "Organization";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 23);
			this.label1.TabIndex = 8;
			this.label1.Text = "Full Name";
			// 
			// textBox8
			// 
			this.textBox8.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.textBox8.Location = new System.Drawing.Point(376, 200);
			this.textBox8.Multiline = true;
			this.textBox8.Name = "textBox8";
			this.textBox8.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox8.Size = new System.Drawing.Size(200, 80);
			this.textBox8.TabIndex = 7;
			this.textBox8.Text = "";
			// 
			// textBox7
			// 
			this.textBox7.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.textBox7.Location = new System.Drawing.Point(88, 192);
			this.textBox7.Multiline = true;
			this.textBox7.Name = "textBox7";
			this.textBox7.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox7.Size = new System.Drawing.Size(184, 104);
			this.textBox7.TabIndex = 6;
			this.textBox7.Text = "";
			// 
			// textBox6
			// 
			this.textBox6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.textBox6.Location = new System.Drawing.Point(376, 128);
			this.textBox6.Multiline = true;
			this.textBox6.Name = "textBox6";
			this.textBox6.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox6.Size = new System.Drawing.Size(200, 48);
			this.textBox6.TabIndex = 5;
			this.textBox6.Text = "";
			// 
			// textBox5
			// 
			this.textBox5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.textBox5.Location = new System.Drawing.Point(88, 128);
			this.textBox5.Multiline = true;
			this.textBox5.Name = "textBox5";
			this.textBox5.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox5.Size = new System.Drawing.Size(184, 48);
			this.textBox5.TabIndex = 4;
			this.textBox5.Text = "";
			// 
			// textBox4
			// 
			this.textBox4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.textBox4.Location = new System.Drawing.Point(376, 64);
			this.textBox4.Multiline = true;
			this.textBox4.Name = "textBox4";
			this.textBox4.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox4.Size = new System.Drawing.Size(200, 48);
			this.textBox4.TabIndex = 3;
			this.textBox4.Text = "";
			// 
			// textBox3
			// 
			this.textBox3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.textBox3.Location = new System.Drawing.Point(88, 64);
			this.textBox3.Multiline = true;
			this.textBox3.Name = "textBox3";
			this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox3.Size = new System.Drawing.Size(184, 48);
			this.textBox3.TabIndex = 2;
			this.textBox3.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.textBox2.Location = new System.Drawing.Point(376, 24);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(200, 21);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.textBox1.Location = new System.Drawing.Point(88, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(184, 21);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// button1
			// 
			this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.button1.Location = new System.Drawing.Point(240, 328);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(104, 23);
			this.button1.TabIndex = 9;
			this.button1.Text = "Add Address";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button3
			// 
			this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.button3.Location = new System.Drawing.Point(264, 328);
			this.button3.Name = "button3";
			this.button3.TabIndex = 9;
			this.button3.Text = "Update";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// Address
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(608, 398);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "Address";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = " Addresses";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

        /// <summary>
        /// Changing some properties of the form in order
        /// to change it from being for adding new address to be
        /// for editing an existing address. Also it assing the data
        /// of the old row to the text boxes in order to enable the 
        /// user to see the old data and change them.
        /// </summary>
        /// <param name="addressData">Array of strings that contains
        /// the data of the old row.
 		///  Address 0 of the array contains the Full Name.
		///  Address 1 of the array contains the Organization.
		///  Address 2 of the array contains the Mobile Numbers Separated by " ; ".
		///  Address 3 of the array contains the Homephone Numbers Separated by " ; ".
		///  Address 4 of the array contains the Workphone Numbers Separated by " ; ".
		///  Address 5 of the array contains the Emails Separated by " ; ".
		///  Address 6 of the array contains the Complete Mail Address.
		///  Address 7 of the array contains Any notes about the owener.</param>
        /// <param name="updateRow">An integer with the index of
        /// the row to be edited</param>

		public void EditFormSetting(string[] addressData, int updateRow)
		{
			// setting the value of the index of the row, to be used in updating
			updateRowIndex = updateRow;

			// assigning text boxes to the address data in order to 
			// enable the user to change them
			textBox1.Text = addressData[0];
			textBox2.Text = addressData[1];
			textBox3.Text = addressData[2];
			textBox4.Text = addressData[3];
			textBox5.Text = addressData[4];
			textBox6.Text = addressData[5];
			textBox7.Text = addressData[6];
			textBox8.Text = addressData[7];

			// showing the button of the update
			button3.Show();

			// hiding the button for the Add
			button1.Hide();
			statusBar1.Hide();

		}

        /// <summary>
        /// Adding new Address to the tables. it calls another method.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		private void button1_Click(object sender, System.EventArgs e)
		{
			// calling method to add the address
			addNewAddress();
		}

        /// <summary>
        /// Closing the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		private void button2_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

        /// <summary>
        /// Updating the data of a row. it first add a new row
        /// with the new data then delete the old row which contains
        /// the old data. It uses the row index to delete the row.
        /// It takes the data from the text boxes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		private void button3_Click(object sender, System.EventArgs e)
		{
			// creating the dataView
			objDatabase.SetDataView("AddressBook","fullName");

			// first adding the new information 
			int status = addNewAddress();
			
			// if it added it then delete the curren row
			// and close and go back to the list addresses
			// else do nothing
            if(status ==1)
			{
				// deleting the old row
				objDatabase.DeleteRow(updateRowIndex);

				this.Close();
			}
		}

        /// <summary>
        /// Add new Address to the database. It makes sure
        /// that the name field was filled by the user. It takes
        /// the data from the text boxes.
        /// </summary>
        /// <returns>1 = the row was added successfuly.
        /// 0 = the row wasn't added.</returns>
		private int addNewAddress()
		{
			// storing all the data into an array to send them
			string[] enteredData = new String[8];

			// checking if the user entered the full name field
			// if not then show error message and go back to the form
			if(textBox1.Text == "")
			{
				MessageBox.Show(" You Must Enter the Full Name Field", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
				// exiting from this method
				return(-1);
			}

			// assinging entered data to the array
			enteredData[0] = textBox1.Text;
			// the following will rmove any "Enter" or double space inside them.
			enteredData[1] = textBox2.Text.Replace("\x000D\x000A","").Replace("  "," ");
			enteredData[2] = textBox3.Text.Replace("\x000D\x000A","").Replace("  "," ");
			enteredData[3] = textBox4.Text.Replace("\x000D\x000A","").Replace("  "," ");
			enteredData[4] = textBox5.Text.Replace("\x000D\x000A","").Replace("  "," ");
			enteredData[5] = textBox6.Text.Replace("\x000D\x000A","").Replace("  "," ");

			enteredData[6] = textBox7.Text;
			enteredData[7] = textBox8.Text;

			// telling the user about the current action
			statusBar1.Text = "Wait: Adding the Address to Database ...";
			Application.DoEvents();

			// calling method with the array to add the new address
			// from NBDatabase class.
			objDatabase.AddNewAddress(enteredData);

			// removing all data from the fields
			cleanAddressBookFields();

			// telling the user about the current action
			statusBar1.Text = "An Address Was Added to the Database.";

			// if successfully added then return 1
			return(1);
		}

		/// <summary>
		/// Making all text fields empty.
		/// </summary>
		private void cleanAddressBookFields()
		{
			textBox1.ResetText();
			textBox2.ResetText();
			textBox3.ResetText();
			textBox4.ResetText();
			textBox5.ResetText();
			textBox6.ResetText();
			textBox7.ResetText();
			textBox8.ResetText();
		}
	}
}

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.


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions