Click here to Skip to main content
15,909,586 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sorry with my english..
i made a button to save textboxes values into xml file.
i made a button to load from xml to each texboxes that saved before

My problem is when i press the loadbutton once yes it loads all the values i saved in xml to each textboxs but after that, i wanted to change some values in one of all textboxs and click the save button again and it pops up window says

"The process cannot access the file 'data.xml' because it is being used by another process."

so i cant modify the data after load..
please can i get solution to make it work! thanks

What I have tried:

Savebutton
private void btnsave_Click_1(object sender, EventArgs e)
        {
           
                    try
                    {
                        Information info = new Information();
                        info.Data1 = txtBasoSU.Text;
                        info.Data2 = txtBasoC.Text;
                        info.Data3 = txtBasoM.Text;
                        info.Data4 = txtBasoB.Text;
                        info.Data5 = txtMieA.Text;
                        info.Data6 = txtMieAB.Text;
                        info.Data7 = txtServisMinuman.Text;
                        info.Data8 = txtServisMakanan.Text;
                        info.Data9 = txtServisBungkus.Text;
                        info.Data10 = txtTotalSpajak.Text;
                        info.Data11 = txtTotalPajak.Text;
                        info.Data12 = txtTotal.Text;

                        SaveXML.SaveData(info, "data.xml");

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

Loadbutton
private void btnload_Click(object sender, EventArgs e)
        {
            if (File.Exists("data.xml"))
            {
                XmlSerializer xs = new XmlSerializer(typeof(Information));
                FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                Information info = (Information)xs.Deserialize(read);

                txtBasoSU.Text = info.Data1;
                txtBasoC.Text = info.Data2;
                txtBasoM.Text = info.Data3;
                txtBasoB.Text = info.Data4;
                txtMieA.Text = info.Data5;
                txtMieAB.Text = info.Data6;

                txtServisMinuman.Text = info.Data7;
                txtServisMakanan.Text = info.Data8;
                txtServisBungkus.Text = info.Data9;
                txtTotalSpajak.Text = info.Data10;
                txtTotalPajak.Text = info.Data11;
                txtTotal.Text = info.Data12;
            }
        }


SaveXML.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;

namespace CS_Sistem_Managemen_Restoran
{
    public class SaveXML
    {
        public static void SaveData(object obj, string filename)
        {
            XmlSerializer sr = new XmlSerializer(obj.GetType());
            TextWriter writer = new StreamWriter(filename);
            sr.Serialize(writer, obj);
            writer.Close();
        }
    }
}


Information.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace CS_Sistem_Managemen_Restoran
{
    public class Information
    {
        private string data1;
        private string data2;
        private string data3;
        private string data4;
        private string data5;
        private string data6;

        private string data7;
        private string data8;
        private string data9;
        private string data10;
        private string data11;
        private string data12;
        
        
        public string Data1
        {
            get { return data1; }
            set { data1 = value; }
        }

        public string Data2
        {
            get { return data2; }
            set { data2 = value; }
        }
        public string Data3
        {
            get { return data3; }
            set { data3 = value; }
        }

        public string Data4
        {
            get { return data4; }
            set { data4 = value; }
        }

        public string Data5
        {
            get { return data5; }
            set { data5 = value; }
        }

        public string Data6
        {
            get { return data6; }
            set { data6 = value; }
        }

        public string Data7
        {
            get { return data7; }
            set { data7 = value; }
        }

        public string Data8
        {
            get { return data8; }
            set { data8 = value; }
        }

        public string Data9
        {
            get { return data9; }
            set { data9 = value; }
        }

        public string Data10
        {
            get { return data10; }
            set { data10 = value; }
        }

        public string Data11
        {
            get { return data11; }
            set { data11 = value; }
        }

        public string Data12
        {
            get { return data12; }
            set { data12 = value; }
        }

        
    }
}
Posted
Updated 12-Aug-18 0:48am

1 solution

Simple: you app is using it, so it can't be opened as it's busy.
When you open a stream, you are responsible for clearing up behind you - closing the stream and Disposing of the object that opened it.
The easiest way is to use a using block:
C#
if (File.Exists("data.xml"))
    {
    XmlSerializer xs = new XmlSerializer(typeof(Information));
    using (FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
        Information info = (Information)xs.Deserialize(read);
        ...
        }  // The stream is closed here for you, and goes out of scope.
    }
 
Share this answer
 
Comments
Muhammad nur Ihsan 12-Aug-18 7:08am    
thanks man im stuck there finding solution about 5hours xD
OriginalGriff 12-Aug-18 7:15am    
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