using System using SystemCollectionsGeneric using SystemComp

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 WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int SIZE = 18;
string[] acct = new string[SIZE];
private void form1_Load(object sender, EventArgs e)
{
try
{
StreamReader inputFile;
inputFile = File.OpenText(\"ChargeAccounts.txt\");

int index = 0;
while (index < acct.Length && !inputFile.EndOfStream)
{
acct[index] = inputFile.ReadLine();

index++;
}
inputFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void CheckButton_Click(object sender, EventArgs e)
{
Boolean valid = false;
for (int i = 0; i < acct.Length; i++)
{
if (acct[i] == InputTextbox.Text)
{
valid = true;
break;
}
}
if (valid == true)
{
MessageBox.Show(\"It is a Valid Charge Account\");
}
else
MessageBox.Show(\"It is Not a Valid Charge Account\");
}
}
}
  
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 WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int SIZE = 18;
string[] acct = new string[SIZE];
private void form1_Load(object sender, EventArgs e)
{
try
{
StreamReader inputFile;
inputFile = File.OpenText(\"ChargeAccounts.txt\");

int index = 0;
while (index < acct.Length && !inputFile.EndOfStream)
{
acct[index] = inputFile.ReadLine();

index++;
}
inputFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void CheckButton_Click(object sender, EventArgs e)
{
Boolean valid = false;
for (int i = 0; i < acct.Length; i++)
{
if (acct[i] == InputTextbox.Text)
{
valid = true;
break;
}
}
if (valid == true)
{
MessageBox.Show(\"It is a Valid Charge Account\");
}
else
MessageBox.Show(\"It is Not a Valid Charge Account\");
}
}
}
  
Problem Charge Account Validation In the Chap07 folder of the Student Sample Programs, you will ind a file named ChargeAccounts.txt. The file contains a list of a company\'s valid charge account numbers. There are a total of 18 charge account numbers in the file, and each one is a 7-digit number, such as 5658845 Create an application that reads the contents of the file into an array or a List. The application should then let the user enter a charge account number. The program should determine whether the number is valid by searching for it in the array or List that contains the valid charge account numbers. If the number is in the array or List, the program should display a message indicating the number is valid. If the number is not in the array or List, the program should display a message indicating the number is invalid.

Solution

There are 2 problem in this code:

First:

Form1.Designer.cs

In InitializeComponent() if there is no Form1_Load event then your code wont execute form1 Load event, so add this in InitializeComponent()

this.Load += new System.EventHandler(this.Form1_Load);

In Form1.cs

change private void form1_Load(object sender, EventArgs e) To private void Form1_Load(object sender, EventArgs e)

as you can observe i have changed form1_Load to Form1_Load

Second Problem:

Please mention the file path

if you just entered the file name the program looks for file in debug folder.

so mention the path for example:

inputFile = File.OpenText(@\"P:\\praticedemo\\Examples\\ChargeAccounts.txt\");

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usi
 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usi
 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site