Unable to update database with DataGridView values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sampalmer21
    New Member
    • Feb 2008
    • 11

    Unable to update database with DataGridView values

    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:

    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();
            }
        }
    }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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.

    Comment

    • sampalmer21
      New Member
      • Feb 2008
      • 11

      #3
      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.

      Comment

      • sampalmer21
        New Member
        • Feb 2008
        • 11

        #4
        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();

        Comment

        Working...