Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I'm a fairly new programmer and I am working on a windows Application in C#. I have created a grid view which is bound to a local DataSet. There is no sql server, this is strictly local DataSet.

I have written the event handler code for the save button that I added on the binding navigator. Can someone please look over my code and let me know where I have gone wrong?



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

        dataTable1BindingSource1.EndEdit();

        App.DataTable1.WriteXml(string.Format("{0}//data.dat", Application.StartupPath));

        MessageBox.Show("Save Complete");
        groupBox3.Enabled = false;
        grpBxReportedBy.Enabled = false;
        groupBox1.Enabled = false;
        App.DataTable1.AcceptChanges();

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Messages", MessageBoxButtons.OK, MessageBoxIcon.Error);
        App.DataTable1.RejectChanges();
    }
}



static DataSet1 db;
protected static DataSet1 App
{
    get
    {
        if (db == null)
            db = new DataSet1();
        return db;
    }
}

private void FascilitiesMaint2018_Load(object sender, EventArgs e)
{

    string fileName = string.Format("{0}//data.dat", Application.StartupPath);
    if (File.Exists(fileName))
    {
        App.DataTable1.ReadXml(fileName);

    }

    dataTable1BindingSource1.DataSource = App.DataTable1;
    groupBox3.Enabled = false;
    grpBxReportedBy.Enabled = false;
    groupBox1.Enabled = false;


}


What I have tried:

I have edited my code numerous times to try to fix the issue but I can't get it to save.
I had a overload error previously which I managed to fix.
I've spent hours trying to look up tutorials on saving with DataSets but all I find are SQL DataSets.
Posted
Comments
CHill60 11-Jul-18 3:52am    
Debug and find the contents of string.Format("{0}//data.dat", Application.StartupPath). That has to be a valid filename and you must be able to write to the folder.
You've set this is windows so try using the Path.Combine method to get the filename - those // should probably be \\ ... Path.Combine Method (String, String) (System.IO)[^]
Andrew Baylis 12-Jul-18 18:35pm    
what is the actual error? Is it to do with the filename or with the actual saving to XML?
gggustafson 13-Jul-18 23:24pm    
What is DataSet1? What is App? If you want to write a DataTable to a CSV file see CSV To.From DataTable here on CP

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