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