473,623 Members | 2,790 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.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:

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 2943
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 SqlCommandBuild er automatically generate those commands. If I delete the SqlCommandBuild er object, then I get a NullReferenceEx ception 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.UpdateC ommand property. This property has to be set to the builder.GetUpda teCommand() value, for example:

adapter.UpdateC ommand = builder.GetUpda teCommand();
Mar 14 '08 #4

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

Similar topics

1
3134
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 datagridview control and SQL express. I tried this code on a separate button to commit changes to the database: Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click If Not Me.DataSet.DataTable.GetChanges...
4
22115
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 = false; //mobiltelefon When I run the programm, the columns are still displayed. I am developing with Visual C#2005 Express Edition Beta.
6
5202
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 table adaptor. I would like to update the b ase tables thorugh the view. How do i do this? FYI: I have some reserarch on how to handle updating multiple tables and making sure I don't break referential integrity, yet in this case I will not be...
3
16692
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 and dragging the exposed view onto the datagrid as per the help instructions I can't get edits to go back to the database. I have tried editing cells at the UI and also programatically. With a UI edit the pencil comes up and goes away when I move...
5
2159
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 change depending how many columns are in a Table. I add a new column with this code: Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Try...
3
3087
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 generated strong typed dataset and binding. And here is my problem. I try to update the database with the new (changes) values from DataGridView:
1
7777
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 course is a priority course or not. Simple. Because there is alot of data stored for each course, I am using a tab control with several pages. The Job Role cross-referencing comes on
1
15149
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 comm.Connection = conn conn.Open()
0
2147
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 this purpose i need for each row loop in datagridview.i dont know how to update the selected row in the datagridview.here my code in asp.net is like this.its working fine.but i want to do this in datagridview in windows application. For...
0
8224
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8667
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8610
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7145
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2597
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 we have to send another system
1
1775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1471
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.