Reputation: 1
I'm creating a simple application to capture customer data from text boxes and save on a text file in drive C, the strings are delimited by commas. And display the stored data on a second form which is activated from the first form by a button The code I've so far written is below can someone please help me where I'm going wrong.
The code compiles without errors, but it displays the warning:
variable FILE in form2 is declared but not used..
. When I start without Debugging it crashes with an error report:
The type initializer for 'InvoiceDataAppGaoria.Form2' threw an exception(Form2 F2=new Form2())
When I assign the array elements to the text boxes the IDE(visual studio 2008) reports an error.
Form2
namespace InvoiceDataAppGaoria
{
public partial class Form2 : Form
{
static string FILE = @"C:\Csharp\coursework1\maucha.txt";
static FileStream outFile = new FileStream("FILE", FileMode.Open, FileAccess.Read);
static StreamReader read = new StreamReader("FILE");
static string line = read.ReadLine();
static string[] values = line.Split(',');
string invoicetxt = values[0];
string lname = values[1];
string fname = values[2];
string AMT = values[3];
public Form2()
{
InitializeComponent();
}
}
}
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace InvoiceDataAppGaoria
{
public partial class Form1 : Form
{
const string DELIM = ",";
const string FILENAME = @"C:\Csharp\coursework1\maucha.txt";
int invoNum;
string lname,fname;
double AMT;
static FileStream outFile = new FileStream("FILENAME",FileMode.Create,FileAccess.Write);
StreamWriter writer = new StreamWriter(outFile);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
invoNum = Convert.ToInt32(invoicetxt.Text);
lname = lnameBox.Text;
fname = fnameBox.Text;
AMT = Convert.ToDouble(amtBox.Text);
writer.WriteLine(invoNum+DELIM+lname+DELIM+fname+DELIM+AMT);
}
private void button2_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.Show();
}
}
}
Upvotes: 0
Views: 7392
Reputation: 20324
When you create the file stream, you use the string "FILE"
instead of the variable FILE
. It should probably look like this instead:
static string FILE = @"C:\Csharp\coursework1\maucha.txt";
static FileStream outFile = new FileStream(FILE, FileMode.Open, FileAccess.Read);
static StreamReader read = new StreamReader(FILE);
And just to have mentioned it: you should really name your variable file
and not FILE
. Sticking to common naming conventions makes reading others code much more pleasant.
Upvotes: 2