Click here to Skip to main content
15,917,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having this problem when i try to save my file... it initially says that there is no object but in the locals i can see the data stored.i tried this code from a tutorial but it seems to fail when i try......... ;(


.. why cant i refer it to the right place. moreover i can save the file but in the end the file ends up empty when i try to open it.








code is this...
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace phone_book
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.ShowDialog();
            saveFileDialog1.RestoreDirectory = true;
            //read aur compare data
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                FileStream output = new FileStream(saveFileDialog1.FileName,
                    FileMode.OpenOrCreate, FileAccess.Write);

                int n = GRID.RowCount;
                data[] person = new data[n - 1];//rows minus one for an empty data line
                for (int i=0; i < n - 1; i++)
                {
                   
                        person[i] = new data();
                        person[i].name = GRID[0, i].Value.ToString();
                        person[i].contact = GRID[1, i].Value.ToString();
                        person[i].phnumber = GRID[2, i].Value.ToString();
                        person[i].address = GRID[3, i].Value.ToString();
                  
                }
                Formatter.Equals(output, person);

                output.Close();
            }
        }

     
    }
    [Serializable] //for saving file
    public class data
    {
        public string name;
        public string contact;
        public string phnumber;
        public string address;
    }
}
Posted
Comments
CPallini 17-Jul-14 6:43am    
You know, the debugger is a magic tool!
Pravuprasad 17-Jul-14 7:25am    
In which line error comes? use debugger for this.

1 solution

It's fairly obvious that you don't really understand the code that you have taken from the internet, as you have a number of method calls that are either superfluous, or just not relevant to the problem in hand. You should spend some time studying some of the MSDN documentation[^] so you have at least a reasonable idea of the required sequence of events.
 
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