I am having a problem with this one error. I have to turn this in tonight and this is holding me up right now. Just not sure how to fix it. .VS 2019 .net framework windows forms
private void DisplayForm_Load(object sender, EventArgs e)
{
if (File.Exists("Employee.txt"))
{
using (StreamWriter sr = new StreamWriter("employee.txt"))
{
string employeeId = "";
while ((employeeId = sr.ReadLine()) != null)
{
Employee employee = new Employee(employeeId, sr.ReadLine(),
decimal.Parse(sr.ReadLine()), decimal.Parse(sr.ReadLine()));
Employee emp = employee;
employeeListBox.Items.Add(emp);
allEmployee.Add(emp);
}
}
}
else
{
employeeListBox.Items.Add("No Employees entered yet.");
}
}
private void printButton_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
int x = 75, y = 75;
foreach (Employee emp in allEmployee)
{
e.Graphics.DrawString(emp.ToString(),
new Font("Courier", 10, FontStyle.Regular),
Brushes.Black, x, y);
y += 14;
}
}
private void closeButton_Click(object sender, EventArgs e)
{
Close();
}
Severity Code Description
Project File Line Suppression
State
Error CS1061 'StreamWriter' does not
contain a definition for 'ReadLine' and no accessible extension
method 'ReadLine' accepting a first argument of type 'StreamWriter'
could be found (are you missing a using directive or an assembly
reference?)
This is the section that I am having problems with
private void DisplayForm_Load(object sender, EventArgs e)
{
if (File.Exists("Employee.txt"))
{
using (StreamWriter sr = new StreamWriter("employee.txt"))
{
string employeeId = "";
while ((employeeId = sr.ReadLine()) != null)
{
Employee employee = new Employee(employeeId, sr.ReadLine(),
decimal.Parse(sr.ReadLine()), decimal.Parse(sr.ReadLine()));
Employee emp = employee;
employeeListBox.Items.Add(emp);
allEmployee.Add(emp);
}
}
}
else
{
employeeListBox.Items.Add("No Employees entered yet.");
}
}
private void printButton_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
int x = 75, y = 75;
foreach (Employee emp in allEmployee)
{
e.Graphics.DrawString(emp.ToString(),
new Font("Courier", 10, FontStyle.Regular),
Brushes.Black, x, y);
y += 14;
}
}
private void closeButton_Click(object sender, EventArgs e)
{
Close();
}
First include the namespace"using System.IO;"
Please use following line instead of StreamWriter. As StreamWriter does not have ReadLine() methods it throws an error.
using (StreamReader sr = new StreamReader("employee.txt"))
As you have provided part of your code, I cud not provide the complete running code. But above two corrections would fix your issue.
Please let me know if you need any further help.
Get Answers For Free
Most questions answered within 1 hours.