Click here to Skip to main content
15,885,855 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void hrs_combobx_SelectedIndexChanged(object sender, EventArgs e)
{
if (File.Exists(@"C:\Users\admin\Documents\combobox1.txt"))
{
using (StreamWriter sw = new StreamWriter(@"C:\Users\admin\Documents\combobox1.txt"))
{
sw.WriteLine(hrs_combobx.SelectedItem);
sw.Close();
}
using (StreamReader sr = new StreamReader(@"C:\Users\admin\Documents\combobox1.txt"))
{
string line1 = sr.ReadLine();
if (line1 != null)
{
hrs_combobx.Text = line1;
line1 = sr.ReadLine();
sr.Close();
}
}
}
}

private void min_combobx_SelectedIndexChanged(object sender, EventArgs e)
{
if (File.Exists(@"C:\Users\admin\Documents\combobox1.txt"))
{
using (StreamWriter sw = new StreamWriter(@"C:\Users\admin\Documents\combobox1.txt"))
{
sw.WriteLine(min_combobx.SelectedItem);
sw.Close();
}
using (StreamReader sr = new StreamReader(@"C:\Users\admin\Documents\combobox1.txt"))
{
string line2 = sr.ReadLine();
if (line2 != null)
{
sec_combobx.Text = line2;
line2 = sr.ReadLine();
sr.Close();
}
}

}
}

private void sec_combobx_SelectedIndexChanged(object sender, EventArgs e)
{
if (File.Exists(@"C:\Users\admin\Documents\combobox1.txt"))
{
using (StreamWriter sw = new StreamWriter(@"C:\Users\admin\Documents\combobox1.txt"))
{
sw.WriteLine(sec_combobx.SelectedItem);
sw.Close();
}
using (StreamReader sr = new StreamReader(@"C:\Users\admin\Documents\combobox1.txt"))
{
string line3 = sr.ReadLine();
if (line3 != null)
{
sec_combobx.Text = line3;
line3 = sr.ReadLine();
sr.Close();
}
}
}
}>

What I have tried:

i have tried stream reader and writer only one value i.e last combobox value is displayed in notepad
Posted
Updated 16-Dec-16 10:37am
v2
Comments
Mohamed Rafiq K 15-Dec-16 4:13am    
I think you are overwriting the files, thats why you are seeing that last written data.
Richard MacCutchan 15-Dec-16 4:50am    
If you do not show us your code we cannot begin to guess what you are doing wrong. Please post proper details of your problem.
hari19113 15-Dec-16 7:19am    
You need to check your FileMode in FileStream. Is it in Append mode? If yes and you are still having problem, please post a code snippet.
Ramza360 16-Dec-16 16:34pm    
Your problem is you are overwriting your file since you are simply creating a StreamWriter and writing a line, then closing the file. You need to specify a StreamWriter with a FileMode as hari19113 stated. Check my example below.

1 solution

Anywhere you are doing the StreamWriter stuff, change it to the following.

Also, since you're utilizing the using() block, there is no need to close the file, the using block of the streamwriter will close it for you.

C#
private void hrs_combobx_SelectedIndexChanged(object sender, EventArgs e)
{
    if (File.Exists(@"C:\Users\admin\Documents\combobox1.txt"))
    {
          using (StreamWriter sw = new StreamWriter(new FileStream(@"C:\Users\admin\Documents\combobox1.txt", FileMode.Append)))
         {
             sw.WriteLine(hrs_combobx.SelectedItem);
         }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900