1

i have a code which contains a local database 'database1.sdf' along with table 'Employees' with some values. I am adding all rows from database to a datagridview using Datagridview.Datasource property. All rows are fetching from table! Fine! when i am trying to update a column value from datagridview , its not updating from the database. same problem happens when deleting row as well as adding new row. There is no exception occurring when running program.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace demoDatabaseOperations
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public SqlCeConnection con = new SqlCeConnection("Data source=Database1.sdf");
    public DataTable dt = new DataTable();

    public SqlCeDataAdapter adptr;
    public SqlCeCommandBuilder cmdBuilder;
    private void Form1_Load(object sender, EventArgs e)
    {

        con.Open();

        if (con.State == ConnectionState.Open)
        {
            adptr = new SqlCeDataAdapter("select * from employees", con);
            cmdBuilder = new SqlCeCommandBuilder(adptr);

            adptr.UpdateCommand = cmdBuilder.GetUpdateCommand();

            adptr.Fill(dt);
            this.dataGridView1.DataSource = dt;

        }
      }

    //Button For Update/delete/insert values to the database
    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            adptr.Update(dt);
            adptr.Dispose();
        }
        catch (Exception f) { MessageBox.Show(f.ToString()); }

    }
}

}

Any one have solution for this ?

Thanks in advance

Lazy Programer
  • 171
  • 1
  • 1
  • 12
  • are you sure the adptr has update instructions? – BugFinder Nov 07 '17 at 11:49
  • See https://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlcedataadapter.updatecommand(v=vs.100).aspx as example - you've not shown any update portion in your code – BugFinder Nov 07 '17 at 11:50
  • @BugFinder we have to manually create update insruction like ' update tablename set column ...' ? or `adptr.UpdateCommand = cmdBuilder.GetUpdateCommand();` this will automatically generate those instruction for update value when changing column from datagridview ? – Lazy Programer Nov 07 '17 at 11:56
  • try removing this line compeletly adptr.UpdateCommand = cmdBuilder.GetUpdateCommand(); – Abdou Nov 07 '17 at 11:57
  • @Abdou Not Working ! Same problem still exist! – Lazy Programer Nov 07 '17 at 12:00
  • you should set a break point just before the call to adptr.Update(dt); and check if the data in the dataTable have really changed – Abdou Nov 07 '17 at 12:03
  • @Abdou , The problem is when i update/delete/insert row, it will update from datagridview. fine, when i reopen the exe will shows all the values recently updated!. i mean the row which is updated/deleted/inserted is showing perfectly alright. when i check inside the database table those updated value will not be shown and the table not at all updated. after checking the database the exe showing the same older database values which is not updated when reopens it, – Lazy Programer Nov 07 '17 at 12:10
  • are you sure you are opening the same database file that the your app is updating to ? – Abdou Nov 07 '17 at 12:18
  • I think I know what is going on. your have put your database file in the root folder of your project and you added it to your project, when you build your project this file is copied to the debug folder and this copied file is the one that your app updates – Abdou Nov 07 '17 at 12:21
  • @Abdou yes Exactly! what is the solution for this? – Lazy Programer Nov 07 '17 at 12:22
  • check your Debug folder for another Database1.sdf file and check if this one is modified. – Abdou Nov 07 '17 at 12:22
  • well you can just put the absolute path to your database in your connection string Data source=C://Database1.sdf – Abdou Nov 07 '17 at 12:24
  • I've copied the solution as an answer, please select it as best answer if it worked for you – Abdou Nov 07 '17 at 12:28

1 Answers1

0

I think I know what is going on. your have put your database file in the root folder of your project and you added it to your project, when you build your project this file is copied to the debug folder and this copied file is the one that your app updates

you can just put the absolute path to your database file in your connection string Data source=C://Database1.sdf

Abdou
  • 330
  • 1
  • 9