Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm reading a file (Filename: Data_Entry.dat) into gridview and modifying the data. After modification, if i save the file with different name then there is no issues.

If i save the file after modifications with same name (Data_Entry.dat), then the modification is existing but the records in the file are getting double. (If file contains 10 records, after saving with same name it's giving 20records).
Below is the code i programmed under save button:


===========Code===================

ClsSaveDat Savdat = new ClsSaveDat();
Savdat.SaveDatArr(datfile);

==================================

Please let me know if i'm unclear about my query or anything. Thanks in advance.
Posted
Comments
Kornfeld Eliyahu Peter 6-Jan-16 3:06am    
Nothing clear about it...
The code you put here tells nothing about how do you save the data, but from your description it seems that you are appending the data to the end of the file you just read...
Member 8010354 6-Jan-16 3:10am    
My apologies for posting a question in unclear format. But what you understood is right!
If i'm trying to save a file with the same filename (The file which i have browsed to view in gridview). It's appending the records. How to resolve it?

1 solution

Without the SaveDat.SaveDatArr method code, we can't tell you what you are doing wrong - because we can't see what you are doing at all!

But...At a guess you are either using File.AppendText or opening a stream for append instead of overwrite:
C#
... = new StreamWriter(@"D:\Temp\Data_Entry.dat", true);
Either change the true to false, or use the single parameter version:
C#
... = new StreamWriter(@"D:\Temp\Data_Entry.dat");
 
Share this answer
 
Comments
Member 8010354 6-Jan-16 4:25am    
Below is the code for methods:

public void SaveDatArr(String[] datfile)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
String StrRebuild = "";
for (int j = 0; j <= datfile.Length - 1; j++)
{
StrRebuild += datfile[j];
StrRebuild += Environment.NewLine;
}

saveFileDialog1.Filter = "dat files (*.DAT)|*.DAT|All files (*.*)|*.*";

saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true);
sw.WriteLine(StrRebuild);
sw.Flush();
sw.Close();
}
}
OriginalGriff 6-Jan-16 4:37am    
And that shows exactly what I said:

StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true);

The "true" parameter says "append this if the file exists".

And please, do yourself a favour and use the StringBuilder class instead of appending strings: it's a lot more efficient, given that strings are immutable!
Also check your indexes: your filter supplies two file types (*.DAT and *.*), but you specify the dialog should use the third index.

Thinking about it, another solution would be to drop the loop completely and just use string.Join:

string strRebuild = string.Join(Environment.NewLine, datfile);
Member 8010354 6-Jan-16 4:45am    
Thanks a lot. Your solution helped me and the point where i strucked is True or False near StreamWriter.
Once again thank you:).
OriginalGriff 6-Jan-16 4:57am    
You're welcome!

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