Click here to Skip to main content
15,919,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i save data in dataGrid and reopen project again after 2 minutes or more project not save data WHY

namespace National_items
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        
        private bool savetag = false;

        private void AdditemBtn_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow row;
                DataTable table = itemsListDataSet.Devlist;
                row = table.NewRow();
                row[0] = textBox1.Text;
                row[1] = textBox2.Text;
                table.Rows.Add(row);
            }
            catch(Exception)
            {
                MessageBox.Show("This Item was inserted", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'itemsListDataSet.Devlist' table. You can move, or remove it, as needed.
            this.devlistTableAdapter.Fill(this.itemsListDataSet.Devlist);
            savetag = true;
        }
        
        private void SavItemBtn_Click(object sender, EventArgs e)
        {
            devlistTableAdapter.Connection.Open();
            devlistTableAdapter.Fill(itemsListDataSet.Devlist);
            itemsListDataSet.GetChanges();
            devlistBindingSource.EndEdit();
            devlistTableAdapter.Connection.Close();
            savetag = true;
        }

        private void DelItemBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
        }
       
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (savetag == false)
            { 
                DialogResult dr = MessageBox.Show("Do you want to save changes", "Confirme", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
                if (dr == DialogResult.Cancel)
                    e.Cancel = true;
                else if (dr == DialogResult.Yes)
                    SavItemBtn_Click(sender, e);
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            savetag = false;
        }
    }
}
Posted
Updated 16-Jan-11 1:24am
v2

I think you will find the asnwer here:
How to: Save Dataset Changes to a Database[^] - this is after all the "official" documentation :)

Regards
Espen Harlinn
 
Share this answer
 
Sorry this is my original code for save btn

but data changes saved for moments just
when i open access database its changed
but after moments when run project again its empty

C#
private void SavItemBtn_Click(object sender, EventArgs e)
        {
            devlistTableAdapter.Connection.Open();
            itemsListDataSet.GetChanges();
            devlistBindingSource.EndEdit();
            devlistTableAdapter.Update(itemsListDataSet.Devlist);
            devlistTableAdapter.Connection.Close();
            savetag = true;
        }
 
Share this answer
 
Comments
Estys 16-Jan-11 8:22am    
Is your question answered or not? If not I have another suggestion.
Espen Harlinn 16-Jan-11 8:22am    
Why not put devlistBindingSource.EndEdit(); on top? it kind of looks strange to GetChanges before calling EndEdit ... just a thought :)
Sendian 17-Jan-11 2:15am    
I create copy from my DB in project folder and connect it with project as wizard ..

But as the same problem when i make any changes in datagrid it's not create any changes on access DB
When you run or debug a project for the first time in VS, it creates the exe AND copies your DB and app.config to the bin directory. Updates to the DB are done to the one in the bin.

Your DB has a property "Copy to Output Directory". If this property has the value "Copy Always" in subsequent runs of your program it always overwrites the DB with the empty one in your project.
So, check the value of the property and change it to "Do not Copy" or "Copy if newer".

Cheers
 
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