Hi,
If I update a cell in the DataGridView control, I use the DataAdapter.Upd ate(DataTable) method to update the database, but when I restart the application my changes are not in the database. Anyone have an idea? Here is my code:
If I update a cell in the DataGridView control, I use the DataAdapter.Upd ate(DataTable) method to update the database, but when I restart the application my changes are not in the database. Anyone have an idea? Here is my code:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
DataTable data;
SqlDataAdapter adapter;
SqlCommandBuilder builder;
SqlConnection conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
conn = new SqlConnection(@"Server=.\SQLEXPRESS;Database=licensedb;Trusted_Connection=yes;");
conn.Open();
SqlCommand cmd = new SqlCommand(@"Select * FROM FeatureTable",conn);
adapter = new SqlDataAdapter(cmd);
data = new DataTable("FeatureTable");
adapter.Fill(data);
builder = new SqlCommandBuilder(adapter);
BindingSource bSource = new BindingSource();
bSource.DataSource = data;
dataGridView1.DataSource = bSource;
String text = builder.GetUpdateCommand().CommandText;
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.EndEdit();
adapter.Update(data);
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
conn.Close();
}
}
}
Comment