473,508 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to update database with DataGridView values

11 New Member
Hi,

If I update a cell in the DataGridView control, I use the DataAdapter.Update(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:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9.  
  10. namespace WindowsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         DataTable data;
  15.         SqlDataAdapter adapter;
  16.         SqlCommandBuilder builder;
  17.         SqlConnection conn;
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.             this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
  27.             conn = new SqlConnection(@"Server=.\SQLEXPRESS;Database=licensedb;Trusted_Connection=yes;");
  28.             conn.Open();
  29.             SqlCommand cmd = new SqlCommand(@"Select * FROM FeatureTable",conn);
  30.             adapter = new SqlDataAdapter(cmd);
  31.             data = new DataTable("FeatureTable");
  32.             adapter.Fill(data);
  33.             builder = new SqlCommandBuilder(adapter);
  34.             BindingSource bSource = new BindingSource();
  35.             bSource.DataSource = data;
  36.             dataGridView1.DataSource = bSource;
  37.             String text = builder.GetUpdateCommand().CommandText;
  38.             dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
  39.         }
  40.  
  41.         void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  42.         {
  43.             dataGridView1.EndEdit();
  44.             adapter.Update(data);
  45.         }
  46.  
  47.         void Form1_FormClosing(object sender, FormClosingEventArgs e)
  48.         {
  49.             conn.Close();
  50.         }
  51.     }
  52. }
Mar 13 '08 #1
3 2926
Plater
7,872 Recognized Expert Expert
Did you ever assign values to the InsertCommand and UpdateCommand of your data adapter? It doesn't look like you did.
You need those to write data back to database.
Mar 13 '08 #2
sampalmer21
11 New Member
But doesn't the SqlCommandBuilder automatically generate those commands. If I delete the SqlCommandBuilder object, then I get a NullReferenceException when I run adapter.Update(DataTable) which implies that it was supplying the commands for the adapter in the first place. So I'm still not sure about why the database doesn't update.
Mar 14 '08 #3
sampalmer21
11 New Member
Thank You Plater!

You were right in that I did not set the adapter.UpdateCommand property. This property has to be set to the builder.GetUpdateCommand() value, for example:

adapter.UpdateCommand = builder.GetUpdateCommand();
Mar 14 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3115
by: Joaquin | last post by:
OK, I hate myself for asking this, it's the worst question I have ever asked but it's worse not to ask: ¿How do I update a datatable in vs 2005? I mean, I am using a typed dataset, a...
4
22096
by: Stropher | last post by:
I have the following: this.dataGridViewBill.DataSource = tblResult; //hide the following columns this.dataGridViewBill.Columns.Visible = false; //email this.dataGridViewBill.Columns.Visible =...
6
5197
by: Greg P | last post by:
I am using VS2005 and have been learning a ton about databinding. I know that when you drag a view from the datasource window (creating a dataGridView) that an update method is not added to the...
3
16687
by: Bob | last post by:
Hi, FrameWork 2.0 Quick and dirty job so I thought I would do it all on the UI as quick and easy as possible. I got the datagridview to display my Access databaseview by using the Datasources...
5
2142
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
3
3079
by: =?Utf-8?B?THVib21pcg==?= | last post by:
Hi, I have a DataGridView that displays data from one table from database. I didn’t implement any my code, I just used wizard to do all the work with DatagridView populating. Visual Studio...
1
7764
by: Karl | last post by:
Hi all... This is a good one. You'll like this... I am working on a course management tool that allows certain Courses to be cross referenced with Job Roles and, when they are, whether the...
1
15138
by: adsaca | last post by:
here's my code in filling a datagridview,which i retrieve from database mysql, dim sql as String sql = "SELECT * FROM table_user" Dim myData As New DataTable ...
0
2138
by: bharathi228 | last post by:
Hi, iam working with windows application.here my requirement is binding data to database from a datagridview. next i want to update some rows in datagridview and update it to the database.for...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.