C# Program Project: I am getting an error on loading my file and getting it to o
ID: 3847987 • Letter: C
Question
C# Program Project:
I am getting an error on loading my file and getting it to output correctly. The assignment is
"Create a project that stores personal information for a little electronic “black book.” The fields in the file should include name, phone number, pager number, cell phone number, voice mail number, and e-mail address. Use text boxes to enter the data and a menu system that will allow the user to load the "black book". Add another form to load the names into a list box and use a structure to hold the fields of data. Perform a “look up” and display the appropriate information for the selected name also Allow the user to select the file to open using the Open File dialog box."
Here is Form 1's Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Chapter11Excercise
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
string strName = textbox1.Text;
string strPhoNo = textbox2.Text;
string strPageNumber = textBox3.Text;
string strCPhNumber = textBox4.Text;
string strVoiceMailNumb = textBox5.Text;
string strEMail = textBox6.Text;
string strFileName = "blackbook.txt";
string inputData = strName + "," + strPhoNo + "," + strCPhNumber + ","
+ strVoiceMailNumb + "," + strEMail + "," + Environment.NewLine;
if (File.Exists(strFileName))
{
File.AppendAllText(strFileName, inputData);
}
else
{
File.WriteAllText(strFileName, inputData);
}
textbox1.Text = "";
textbox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void loadBlackBookToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
Here is Form 1s Image:
Here is Form 2 Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Chapter11Excercise
{
struct bookDetails
{
public string Name;
public string PhNo;
public string PageNumber;
public string CellPhoneNumber;
public string VoiceMailNumber;
public string EMail;
}
public partial class Form2 : Form
{
List<bookDetails> lst = new List<bookDetails>();
public Form2()
{
InitializeComponent();
string strFileName = "blackbook.txt";
string[] lines = File.ReadAllLines(strFileName);
foreach (string strLine in lines)
{
bookDetails bt = new bookDetails();
bt.Name = strLine.Split(',')[0];
bt.PhNo = strLine.Split(',')[1];
bt.PageNumber = strLine.Split(',')[2];
bt.CellPhoneNumber = strLine.Split(',')[3];
bt.VoiceMailNumber = strLine.Split(',')[4];
bt.EMail = strLine.Split(',')[5];
lst.Add(bt);
blackBookListBox.Items.Add(strLine.Split(',')[0]);
}
}
private void blackBookListBox_SelectedIndexChanged(object sender, EventArgs e)
{
string strName = blackBookListBox.SelectedItem.ToString();
bookDetails dt = lst.Where(x => x.Name == strName).FirstOrDefault();
label1.Text = "Name : " + dt.Name;
label2.Text = "PhoneNumber : " + dt.PhNo;
label3.Text = "Page Number : " + dt.PageNumber;
label4.Text = "Cell Ph No : " + dt.CellPhoneNumber;
label5.Text = "Voice email : " + dt.VoiceMailNumber;
label6.Text = "voice Ph No : " + dt.EMail;
}
}
}
Here is frm 2 image:
It will not allow form 2 to load because of a out of range exception I am getting.
Thank You
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Chapter11Excercise
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
string strName = textbox1.Text;
string strPhoNo = textbox2.Text;
string strPageNumber = textBox3.Text;
string strCPhNumber = textBox4.Text;
string strVoiceMailNumb = textBox5.Text;
string strEMail = textBox6.Text;
string strFileName = "blackbook.txt";
string inputData = strName + "," + strPhoNo + "," + strCPhNumber + ","
+ strVoiceMailNumb + "," + strEMail + "," + Environment.NewLine;
if (File.Exists(strFileName))
{
File.AppendAllText(strFileName, inputData);
}
else
{
File.WriteAllText(strFileName, inputData);
}
textbox1.Text = "";
textbox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void loadBlackBookToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
Explanation / Answer
Hi,
From what i could see, your code is fully correct, the only chance to get an exception would be here,
foreach (string strLine in lines)
{
bookDetails bt = new bookDetails();
bt.Name = strLine.Split(',')[0];
bt.PhNo = strLine.Split(',')[1];
bt.PageNumber = strLine.Split(',')[2];
bt.CellPhoneNumber = strLine.Split(',')[3];
bt.VoiceMailNumber = strLine.Split(',')[4];
bt.EMail = strLine.Split(',')[5];
lst.Add(bt);
blackBookListBox.Items.Add(strLine.Split(',')[0]);
}
Here, if the text file does not have proper data split according to , i.e 6 fileds seperated by comma, then you will get out of range exception. i suggest you to use it like this,
foreach (string strLine in lines)
{
if(strLine.Split(',').Length()<6) // less than number of fields
throw exception;
bookDetails bt = new bookDetails();
bt.Name = strLine.Split(',')[i];
bt.PhNo = strLine.Split(',')[1];
bt.PageNumber = strLine.Split(',')[2];
bt.CellPhoneNumber = strLine.Split(',')[3];
bt.VoiceMailNumber = strLine.Split(',')[4];
bt.EMail = strLine.Split(',')[5];
lst.Add(bt);
blackBookListBox.Items.Add(strLine.Split(',')[0]);
}
Thumbs up if this was helpful, otherwise let me know in comments.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.