Reputation: 171
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
Upvotes: 1
Views: 314
Reputation: 330
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
Upvotes: 0