Records Management |
|
Once a table has been filled with records, you can perform maintenance operations on it such as changing some records or removing others. Editing a record consists of changing one of the values of the record under a particular column. There are various ways you can do this. For a console application, the general steps you can (we will) follow are:
To perform these steps, you use a combination of the techniques we has reviewed so far: locate the table, display the records, locate the record, locate the column, assign the value to the column of a record, save the table. |
Editing a Record by the Columns' Object Names |
In the previous lesson, we saw that you could isolate a record based on the object names of the columns such as Director or Title for a table of videos. Once you have identified a column for a record, you can assign the desired value. Here are examples: |
using System; using System.IO; using System.Xml; using System.Data; using System.Collections; namespace VideoCollection3 { public class VideoCollection { public DataSet dsVideos; private DataColumn colShelfNumber; private DataColumn colTitle; private DataColumn colDirector; private DataColumn colYear; private DataColumn colLength; private DataColumn colRating; private DataTable tblVideos; // These are accessory strings string strDirectory; string strFilename; public VideoCollection() { CreateCollection(); strDirectory = @"C:\Programs\Video Collection"; strFilename = strDirectory + "\\" + "videos.xml"; DirectoryInfo dirInfo = new DirectoryInfo(strDirectory); // If the folder doesn't exist already, create it if (!dirInfo.Exists) dirInfo.Create(); } public void CreateCollection() { dsVideos = new DataSet("Videos"); tblVideos = new DataTable("Video"); colShelfNumber = new DataColumn("ShelfNumber", Type.GetType("System.String")); tblVideos.Columns.Add(colShelfNumber); colTitle = new DataColumn("Title", Type.GetType("System.String")); tblVideos.Columns.Add(colTitle); colDirector = new DataColumn("Director", Type.GetType("System.String")); tblVideos.Columns.Add(colDirector); colYear = new DataColumn("Year", Type.GetType("System.Int32")); tblVideos.Columns.Add(colYear); colLength = new DataColumn("Length", Type.GetType("System.String")); tblVideos.Columns.Add(colLength); colRating = new DataColumn("Rating", Type.GetType("System.String")); tblVideos.Columns.Add(colRating); dsVideos.Tables.Add(tblVideos); } public void ShowTables() { int i = 1; Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection - Tables"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); foreach (DataTable tbl in dsVideos.Tables) Console.WriteLine("{0}. {1}", i++, tbl.TableName); Console.WriteLine("----------------------------"); } public void ShowColumns(string table) { int i = 1; DataTable tbl = dsVideos.Tables[table]; Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection - {0} Columns", table); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); foreach (DataColumn col in tbl.Columns) Console.WriteLine("{0}. {1}", i++, col.ColumnName); Console.WriteLine("----------------------------"); } public void CreateVideo() { string ShelfNumber, Title, Director, Length; int Year; string Rating;// ratings; // = { "G", "PG", "PG-13", "R", "NC-17", "N/R" }; Console.WriteLine("Enter the following pieces of information about the video"); Console.Write("Shelf Number: "); ShelfNumber = Console.ReadLine(); XmlDocument xmlVideos = new XmlDocument(); if (File.Exists(strFilename)) { xmlVideos.Load(strFilename); dsVideos.ReadXml(strFilename); XmlElement nodRoot = xmlVideos.DocumentElement; XmlNodeList nodShelfNumbers = nodRoot.GetElementsByTagName("ShelfNumber"); foreach (XmlNode nodShelfNumber in nodShelfNumbers) { if (nodShelfNumber.InnerText == ShelfNumber) { Console.WriteLine("The shelf number {0} exists already", ShelfNumber); return; } } } else dsVideos.WriteXml(strFilename); Console.Write("Title: "); Title = Console.ReadLine(); Console.Write("Director: "); Director = Console.ReadLine(); Console.Write("Year Released: "); Year = int.Parse(Console.ReadLine()); Console.Write("Length (ex 118mins): "); Length = Console.ReadLine(); Console.WriteLine("Specify corresponding rating"); Console.WriteLine("G"); Console.WriteLine("PG"); Console.WriteLine("PG-13"); Console.WriteLine("R"); Console.WriteLine("NC-17"); Console.WriteLine("N/R"); Console.Write("Your Choice? "); Rating = Console.ReadLine(); DataRow rowVideo = this.tblVideos.NewRow(); rowVideo[0] = ShelfNumber; rowVideo[1] = Title; rowVideo[2] = Director; rowVideo[3] = Year; rowVideo[4] = Length; rowVideo[5] = Rating; this.tblVideos.Rows.Add(rowVideo); this.dsVideos.WriteXml(strFilename); } public void ShowVideos() { if (File.Exists(strFilename)) { dsVideos.ReadXml(strFilename); Console.WriteLine("========================================"); Console.WriteLine(" Video Collection"); Console.WriteLine("========================================="); foreach (DataRow vdo in tblVideos.Rows) { Console.WriteLine("Shelf #: {0}", vdo["ShelfNumber"]); Console.WriteLine("Title: {0}", vdo["Title"]); Console.WriteLine("Director: {0}", vdo["Director"]); Console.WriteLine("(c) Year: {0}", vdo["Year"]); Console.WriteLine("Length: {0:C}", vdo["Length"]); Console.WriteLine("Rating: {0}", vdo["Rating"]); Console.WriteLine("-----------------------------------------"); } } } public void EditVideo() { if (File.Exists(strFilename)) { bool found = false; int iYear = 0; string strShelfNumber = "AA-000", strTitle = "Unknown", strDirector = "Unknown", strLength = "N/A", strRating = "N/A"; dsVideos.ReadXml(strFilename); DataRow rowVideo = null; Console.WriteLine("\nHere is the current list of videos"); Console.WriteLine("========================================"); Console.WriteLine(" Video Collection"); Console.WriteLine("========================================="); foreach (DataRow vdo in tblVideos.Rows) { Console.WriteLine("Shelf #: {0}", vdo["ShelfNumber"]); Console.WriteLine("Title: {0}", vdo["Title"]); Console.WriteLine("Director: {0}", vdo["Director"]); Console.WriteLine("(c) Year: {0}", vdo["Year"]); Console.WriteLine("Length: {0:C}", vdo["Length"]); Console.WriteLine("Rating: {0}", vdo["Rating"]); Console.WriteLine("-----------------------------------------"); } Console.Write("Enter the shelf number of the video you want to edit: "); strShelfNumber = Console.ReadLine(); foreach (DataRow vdo in tblVideos.Rows) { string str = (string)vdo["ShelfNumber"]; Console.WriteLine(str); if( str == strShelfNumber ) { //rowVideo = vdo; found = true; Console.WriteLine("\n-----------------------------------------"); Console.WriteLine("Here is the video"); Console.WriteLine("1. Title: {0}", vdo["Title"]); Console.WriteLine("2. Director: {0}", vdo["Director"]); Console.WriteLine("3. (c) Year: {0}", vdo["Year"]); Console.WriteLine("4. Length: {0}", vdo["Length"]); Console.WriteLine("5. Rating: {0}", vdo["Rating"]); Console.WriteLine("-----------------------------------------"); strTitle = (string)vdo["Title"]; strDirector = (string)vdo["Director"]; iYear = (int)vdo["Year"]; strLength = (string)vdo["Length"]; strRating = (string)vdo["Rating"]; Console.Write("Enter the index of the column " + "whose value you want to change: "); int col = int.Parse(Console.ReadLine()); switch (col) { case 1: vdo["ShelfNumber"] = strShelfNumber; Console.Write("Enter the new video title: "); strTitle = Console.ReadLine(); vdo["Title"] = strTitle; vdo["Director"] = strDirector; vdo["Year"] = iYear; vdo["Length"] = strLength; vdo["Rating"] = strRating; this.dsVideos.WriteXml(strFilename); break; case 2: vdo["ShelfNumber"] = strShelfNumber; vdo["Title"] = strTitle; Console.Write("Enter the new director of the video: "); strDirector = Console.ReadLine(); vdo["Director"] = strDirector; vdo["Year"] = iYear; vdo["Length"] = strLength; vdo["Rating"] = strRating; this.dsVideos.WriteXml(strFilename); break; case 3: vdo["ShelfNumber"] = strShelfNumber; vdo["Title"] = strTitle; vdo["Director"] = strDirector; Console.Write("Enter the right year released of the video: "); iYear = int.Parse( Console.ReadLine()); vdo["Year"] = iYear; vdo["Length"] = strLength; vdo["Rating"] = strRating; this.dsVideos.WriteXml(strFilename); break; case 4: vdo["ShelfNumber"] = strShelfNumber; vdo["Title"] = strTitle; vdo["Director"] = strDirector; vdo["Year"] = iYear; Console.Write("Enter the new length of the video: "); strLength = Console.ReadLine(); vdo["Length"] = strLength; vdo["Rating"] = strRating; this.dsVideos.WriteXml(strFilename); break; case 5: vdo["ShelfNumber"] = strShelfNumber; vdo["Title"] = strTitle; vdo["Director"] = strDirector; vdo["Year"] = iYear; vdo["Length"] = strLength; Console.Write("Enter the right rating for the video: "); strRating = Console.ReadLine(); vdo["Rating"] = strRating; this.dsVideos.WriteXml(strFilename); break; } return; } } if (found == false) { Console.WriteLine("No video with that shelf number was found"); return; } } } } }
Editing a Record by the Columns' Indices |
We saw that another technique of recognizing a record was by using the index of each column applied to the DataRow object of the record. You can apply this concept to identify each column. Once you do, you can then assign the desired value.
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|