Hi,
I have a filestream which reads in and write out to a file perfectly:
FileStream strm = new FileStream("Success.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader SR = new StreamReader(strm);
success.Text = SR.ReadToEnd();
success.Text = success.Text.Replace("<br/>", "\r\n");
SR.Close();
strm.Close();
FileStream strm2 = new FileStream("Reject.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader SR2 = new StreamReader(strm2);
reject.Text = SR2.ReadToEnd();
reject.Text = reject.Text.Replace("<br/>", "\r\n");
SR2.Close();
strm.Close();
However when I try and call this file again to read it, it says doesn't read anything and says it is the end of the string. Before anyone says this is the content of the file: "Hello [name]<br/><br/>your in [city]>I am ok thank you!pdhfpsd"
I use the exact same code the second time round:
FileStream strm3 = new FileStream("Success.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader SR3 = new StreamReader(strm3);
string success = SR3.ReadToEnd();
SR3.Close();
strm3.Close();
I'm stumped as to why the stream is reading 'endofstream', anybody has any ideas
Thanks
Daniel

Filestream problems
Kolja
Thanks for trying but still no luck!
I thought i would try the code by calling functions in another class but that doesn't work either. There is just something about that loop which makes it not want to read the files. I can open the form up through main and it works perfectly, but it will not work perfectly from where i am calling it from.
To get around this i tried using a dataset (since Im using them anyway) but that was just loads of hard work, and didn't work (PK constraints), does anybody have any ideas of how i can store saved information
I've included my new code just incase you guys want to see if there are any problems:
public string GetSuccess()
{
MailEdit open = new MailEdit();
string s = open.getsucc();
return s;
}
public string GetReject()
{
MailEdit open = new MailEdit();
string s = open.getrej();
return s;
}
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Step
{
public partial class MailEdit : Form
{
public MailEdit()
{
InitializeComponent();
success.Text = getsucc();
reject.Text = getrej();
}
private void button1_Click(object sender, EventArgs e)
{
using (FileStream strm = new FileStream("Success.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamWriter SW = new StreamWriter(strm);
SW.Write(success.Text);
}
using (FileStream strm = new FileStream("Reject.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamWriter SW = new StreamWriter(strm);
SW.Write(reject.Text);
}
Close();
}
public string getsucc()
{
string dummy = "";
using(FileStream strm = new FileStream("Success.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamReader SR = new StreamReader(strm);
dummy = SR.ReadLine();
}
return dummy;
}
public string getrej()
{
string dummy = "";
using (FileStream strm = new FileStream("Reject.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamReader SR = new StreamReader(strm);
dummy = SR.ReadToEnd();
SR.Dispose();
}
return dummy;
}
}
}
kcchesnut
James_Steven
The kind of database you are using not affect this at all.
Still I see nothing obviously wrong... although in GetSuccess() you are never explicitly closing the file as you are in GetReject()... hum.
One other little crazy thought... In GetSuccess() you use a different access mode than in GetReject()... if you do not need write access, you may want t consider going back to FileMode.Open and FileAccess.Read.
ashk1860
arefsoft
I thought that to! Thats why I renamed the streamreader something different, but i make sure that i close the streamreader after i have finished with it. However one thought i do have is that i'm executing it in a big heap of code where i also access the microsoft JET database to gain access to Excel - would that have anything to do with it Just to be on the safe side here's the entire code listing for that particular file:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Step
{
/// <summary>
/// This code handles the excel file. Created by Daniel Saxil-Nielsen
/// </summary>
class ExcelHandling
{
public void getData(string Location, StepDataSet Step, bool Email)
{
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=" + Location + @";
Extended Properties=""Excel 8.0;HDR=YES;""";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
int count = 0;
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT ApplicationID, StudentID, AppEligibleCurrentYear, AppForename, AppSurname, AppAddress1, AppAddress2, AppAddress3, AppCity, Email, Phone, AppPostCode, AppTermAddress1, AppTermAddress2, AppTermAddress3, AppTermCity, AppTermPostCode, AppDOB, AppUniversityName, AppUniversityNameNotListed, AppCourseName, AppQualSubject, AppQualResult, AppLanguageSkills, AppPublicTransport, AppDrivingLicense, AppVehicleAccess, AppAttitude, AppLeisure, AppComputerSkills, AppWorkExperience, AppMainSkills, AppStudentNotes, AppliedDate FROM [Student_Applications$]";
connection.Open();
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read() && !dr.IsDBNull(0))
{
if (Step.Students.FindByApplicationID(Int32.Parse(dr["ApplicationID"].ToString())) == null)
{
Step.Students.AddStudentsRow(
Int32.Parse(dr["ApplicationID"].ToString()),
Int32.Parse(dr["StudentID"].ToString()),
0,
Int32.Parse(dr["AppEligibleCurrentYear"].ToString()),
(dr["AppForename"].ToString()), (dr["AppSurname"].ToString()),
(dr["AppAddress1"].ToString()), (dr["AppAddress2"].ToString()),
(dr["AppAddress3"].ToString()), (dr["AppCity"].ToString()),
(dr["Email"].ToString()), (dr["Phone"].ToString()),
(dr["AppPostCode"].ToString()), (dr["AppTermAddress1"].ToString()),
(dr["AppTermAddress2"].ToString()), (dr["AppTermAddress3"].ToString()),
(dr["AppTermCity"].ToString()), (dr["AppTermPostCode"].ToString()),
(dr["AppDOB"].ToString()), (dr["AppUniversityName"].ToString()),
(dr["AppUniversityNameNotListed"].ToString()), (dr["AppCourseName"].ToString()),
(dr["AppQualSubject"].ToString()), (dr["AppQualResult"].ToString()),
(dr["AppLanguageSkills"].ToString()), Int32.Parse(dr["AppPublicTransport"].ToString()),
Int32.Parse(dr["AppDrivingLicense"].ToString()), Int32.Parse(dr["AppVehicleAccess"].ToString()),
(dr["AppAttitude"].ToString()), (dr["AppLeisure"].ToString()),
(dr["AppComputerSkills"].ToString()), (dr["AppWorkExperience"].ToString()),
(dr["AppMainSkills"].ToString()), (dr["AppStudentNotes"].ToString()),
DateTime.Parse(dr["AppliedDate"].ToString()), DateTime.Now, false, "");
count++;
Mail Msg = new Mail();
string preface = dr["AppPostCode"].ToString().Substring(0, 2).ToUpper();
Step.PostCode.AcceptChanges();
if (Step.PostCode.FindByPostCode(preface) == null)
{
PostCode PostCodeCheck = new PostCode(preface, Step);
if (PostCodeCheck.ShowDialog() == DialogResult.OK)
{
if (Email == true)
{
string mail = GetSuccess();
mail.Replace("[name]", dr["AppForename"].ToString()).Replace("[city]", dr["AppCity"].ToString());
Msg.send(dr["Email"].ToString(), "Welcome to Shell Step", mail);
}
try
{
Step.PostCode.AddPostCodeRow(preface);
}
catch
{
}
}
else
{
if (Email == true)
{
string mail = GetReject();
mail.Replace("[name]", dr["AppForename"].ToString()).Replace("[city]", dr["AppCity"].ToString());
Msg.send(dr["Email"].ToString(), "There has been a problem with your application", mail);
}
}
}
else
{
if (Email == true)
{
string mail = GetSuccess();
mail.Replace("[name]", dr["AppForename"].ToString()).Replace("[city]", dr["AppCity"].ToString());
Msg.send(dr["Email"].ToString(), "Welcome to Shell Step", mail);
}
}
}
}
}
}
}
MessageBox.Show(count + " students were successfully added to the database.", "Update Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public string GetSuccess()
{
FileStream strm = new FileStream("Success.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader SR = new StreamReader(strm);
string dummy = SR.ReadToEnd();
if (dummy == null)
{
DialogResult Error = MessageBox.Show("There has been an error, QuickStep will now terminate.\r\n\r\nEmail template files contain no information, please locate Success.txt and Reject.txt and edit them accordingly", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (Error == DialogResult.OK)
System.Environment.Exit(1);
}
return dummy;
}
public string GetReject()
{
FileStream file = new FileStream("Reject.qsf", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
sr.Close();
file.Close();
if (s == "")
{
DialogResult Error = MessageBox.Show("There has been an error, QuickStep will now terminate.\r\n\r\nEmail template files contain no information, please locate Success.txt and Reject.txt and edit them accordingly", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (Error == DialogResult.OK)
System.Environment.Exit(1);
}
return s;
}
}
}
Thomas S. Andersen
Brenden is right,
You should always close the file so that its cursor position should get back to 0.
To do this there are 2 ways:
using(FileStream strm = new FileStream("Success.qsf", FileMode.OpenOrCreate, FileAccess.ReadWrite);)
{
StreamReader SR = new StreamReader(strm);
string dummy = SR.ReadToEnd();
}
This is better and recomended way to do it.
I hope this will solve your problem.
Best Regards,
Rizwan aka RizwanSharp