Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can write a new .csv file when the app opens but if I want to edit a existing .csv file and rewrite it I get a blank datagride. how do I get around that?

What I have tried:

C#
       private void btnWrite_Click(object sender, EventArgs e)
        {
           

                using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "csv|*.csv", ValidateNames = true })
                {
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        using (var sw = new StreamWriter(sfd.FileName))
                        {
                            var writer = new CsvWriter(sw);
                            writer.WriteHeader(typeof(Lunch));

                            if (lunchBindingSource == null || lunchBindingSource.DataSource == null)
                            {
                                MessageBox.Show("Error in Saving.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }


                            foreach (Lunch s in lunchBindingSource.DataSource as List<Lunch>)
                            {
                                writer.WriteRecord(s);
                            }
                        }
                        MessageBox.Show("Your Data has been Successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
        }
         

private void btnRead_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    
                        var sr = new StreamReader(new FileStream(ofd.FileName, FileMode.Open));
                        var csv = new CsvReader(sr);
                          lunchBindingSource.DataSource = csv.GetRecords<Lunch>().ToString();
                        lunchBindingSource.DataSource = csv.GetRecords<Lunch>(); /// use to create file
                    }
               }
            }
Posted
Updated 14-Mar-17 0:54am
v3
Comments
[no name] 10-Feb-17 20:23pm    
The very first thing for you to do is to stop ignoring exceptions.
The second thing for you to do is to learn how to debug your code. And, really do it this time.
[no name] 11-Feb-17 10:19am    
https://www.codeproject.com/Questions/1169546/Null-reference-when-writing-to-csv-file

Again, what is your question? Reposting the same thing over and over isn't going to help you. Ignoring exceptions aren't going to help you either. You need to find out what the error means and fix it. You already know where it occurs and why now you need to fix it.
[no name] 11-Feb-17 20:10pm    
You learn how to use the debugger, find the object that is null and fix your error.
Karthik_Mahalingam 10-Feb-17 22:16pm    
Always use  Reply   button to post comments/query to the concerned user, so that the user gets notified and respond to your text.

Start by (as NotPoliticallyCorrect says) not ignoring exceptions.
When you write code like this:
C#
try
{
	... code ...
}
catch (Exception ex)
{
}
You are deliberately throwing away any information that might be available on what is causing a problem. It's a bit like getting a puncture in the car and just turning the music up to drown out the noise - it will come back to bite you later, and a lot harder than if you had stopped and looked at the problem!
So add code to your catch block to report the problem - a MessageBox for example (if this is a WinForms or WPF app).
And then put a breakpoint on the using line in your Read button click handler and step through the code in the debugger to see exactly what is happening and when.

You need information - and we can't get it for you, as we don't have access to your data!
 
Share this answer
 
As you have been told, remove the try/catch from your code.
The try/catch is here to hide failures in your code, and the reason and the position of failure.
Advice: never use try/catch until you know exactly why you do it. And use it until you finished debugging your code.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to.
 
Share this answer
 
I added this piece of code and fixed the issue.

C#
lunchBindingSource.ResetBindings(false);
                 
                        var lunchList = lunchBindingSource.List as IList<Lunch>;
                        foreach (Lunch s in lunchList)
                        {
                            writer.WriteRecord(s);
                        }
 
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