Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every body
i have a strange problem i build a project in ms access and c# i was working fine no problem once i work on it i discover a strange thing all data is lost on data in the table
i try to add data from my form again to follow and find what happen i find that it works fine and save data when i close the project and open access database through visual or access i find its lost again i add data from access and close it and open it again i find data is still there when i open the project from visual it get lost the most strange that happen for only one table not all tables the form which use this table code is

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

    try
    {
        _con = new OleDbConnection(_cs);
        _con.Open();
        string newVariable =
           string.Format(@"SELECT  top 1 Tbl_UserDetails.Installment_Number FROM Tbl_Users INNER JOIN Tbl_UserDetails ON Tbl_Users.USer_ID = Tbl_UserDetails.USer_ID  WHERE Tbl_Users.Name='{0}' ORDER BY Tbl_UserDetails.Installment_date DESC", txtname.Text);
        _cmd = new OleDbCommand(newVariable, _con);_ds = new DataSet();
        _da = new OleDbDataAdapter(_cmd);_da.Fill(_ds, "Tbl_InstallmentCount");
        if (_ds.Tables[0].Rows.Count != 0)
        {_installNumber = Convert.ToInt32(_ds.Tables[0].Rows[0]["Installment_Number"]);
        }

        if (_installNumber == 0)
        {
            _installNumber = 1;
        }else
        {
            _installNumber += 1;
        }
        string cb =
            string.Format(@"insert into Tbl_UserDetails( USer_ID, Installment_Number, Installment_previous_Rest,Installment_Change,Installment_Total, Installment_date) VALUES ({0},{1},{2},{3},{4},{5})",
                _x, _installNumber, txttotal.Text, txtpay.Text, txtsumtotal.Text, string.Format("#{0}#", dateTimePicker1.Value.ToShortDateString()));

        _cmd = new OleDbCommand(cb) {Connection = _con};
        _cmd.ExecuteReader();
        _con.Close();
        XtraMessageBox.Show(@"added", "add", MessageBoxButtons.OK,
            MessageBoxIcon.Information);
        Clear();


        if (_installNumber != 0)
        {_installNumber = 0;
        }

        gridControl1.DataSource = null;
    }
    catch (Exception ex)
    {
        XtraMessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}



where is the problem (My code - office - windows or what ??)
Posted
Comments
Dave Kreskowiak 18-Jun-15 18:28pm    
How are you checking to see if the data is in the database?
Are you running this code in the debugger inside Visual Studio when this happens?
Where is the database that's being updated?
Is it in the project folder or somewhere under the bin folder of your project?
When exactly are you checking to see if the data disappeared?
Are you looking directly at the database contents in Access?
Does the data disappear immediately after the application closes or are you relying on the next launch of your application to tell you that the data disappeared or what?
ost3z 18-Jun-15 18:54pm    
thanks for reply my database folder is under bin folder i really debug my project many time every thing in this form works exactly as i need when i close that form every data lost in this only table not other despite there is no code on close thanks again for u

1 solution

Your data is being lost because every time you run the project in Visual Studio, the database is being copied from the project back down to the bin folder, OVERWRITING ANY CHANGES YOU MADE IN THE PREVIOUS RUN. You're are restoring the database to its starting state when you added it to the project every time you run your project.
 
Share this answer
 
Comments
ost3z 18-Jun-15 19:34pm    
your answer is logic what is the best solution for that problem ?
Dave Kreskowiak 18-Jun-15 19:45pm    
Well, the first is don't use Access. The second is to click on the database file in Solution Explorer, look in the Properties pane and set the Copy to Output Directory to "Copy if newer". Then the file won't get overwritten unless you change the original database file in the project folder (above the bin folder).

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