We help IT Professionals succeed at work.

How to commit changes when displaying datagrid in C#

rwheeler23 asked
I have a datagrid upon which users can make changes. I am now trying to have those changes comitted to the database and I am having trouble determining where in the code I can place the commit command. I cannot seem to find the correct scope of where to put it. Can you please let me know the exact command and location? I was trying to put it in the Exit event but then it does not recognize my dataset object.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace APIMPORT
    public partial class ShowAPIMPORT : Form
        public ShowAPIMPORT()
            DataTable APImportTable = new DataTable();
            string connStr = "Data Source=SQLHOME;Initial Catalog=PLLC;User ID=sa;Password=223";
            string query = "Select BatchID as 'Batch ID',DocumentType as 'Document Type',DocumentDate as 'Document Date',Description,VendorID as 'Vendor ID', DocumentNumber as 'Document Number', convert(decimal(19,2),DocumentAmount) as 'Document Amount',DistributionCode as 'Distribution Code',DistributionDescription as 'Distribution Description',DistributionType as 'Distribution Type',convert(decimal(19,2),DistributionAmount) as 'Amount' from APIMPORT";
            SqlConnection conn = new SqlConnection(connStr);
            SqlDataAdapter rst = new SqlDataAdapter(query, connStr);
                dataGridViewAPIMPORT.DataSource = APImportTable.DefaultView;
                dataGridViewAPIMPORT.Columns[0].Width = 80;
                dataGridViewAPIMPORT.Columns[0].HeaderText = "Batch ID";
                dataGridViewAPIMPORT.Columns[1].Width = 75;
                dataGridViewAPIMPORT.Columns[1].HeaderText = "Doc Type";
                dataGridViewAPIMPORT.Columns[2].Width = 80;
                dataGridViewAPIMPORT.Columns[2].HeaderText = "Doc Date";
                dataGridViewAPIMPORT.Columns[3].Width = 100;
                dataGridViewAPIMPORT.Columns[3].HeaderText = "Description";
                dataGridViewAPIMPORT.Columns[4].Width = 80;
                dataGridViewAPIMPORT.Columns[4].HeaderText = "Vendor ID";
                dataGridViewAPIMPORT.Columns[5].Width = 110;
                dataGridViewAPIMPORT.Columns[5].HeaderText = "Doc Number";
                dataGridViewAPIMPORT.Columns[6].Width = 75;
                dataGridViewAPIMPORT.Columns[6].HeaderText = "Doc Amount";
                dataGridViewAPIMPORT.Columns[6].DefaultCellStyle.Format = "$###,##0.00";
                dataGridViewAPIMPORT.Columns[7].Width = 110;
                dataGridViewAPIMPORT.Columns[7].HeaderText = "Dist Code";
                dataGridViewAPIMPORT.Columns[8].Width = 75;
                dataGridViewAPIMPORT.Columns[8].HeaderText = "Dist Desc";
                dataGridViewAPIMPORT.Columns[9].Width = 75;
                dataGridViewAPIMPORT.Columns[9].HeaderText = "Dist Type";
                dataGridViewAPIMPORT.Columns[10].Width = 80;
                dataGridViewAPIMPORT.Columns[10].HeaderText = "Dist Amount";
                dataGridViewAPIMPORT.Columns[10].DefaultCellStyle.Format = "$###,##0.00";
            catch(Exception ex)
                MessageBox.Show("Exception: " + ex);
                // Close the connection
                if (conn != null)
        private void buttonExit_Click(object sender, EventArgs e)

Open in new window

Watch Question



I rephrased question title.
Your problem is, that you use local variables to store the object references.

Move the definition of  the DataTable and the SqlDataAdapter out of the Show function to the scope of the class.

Now you can access the the members in the ButtenExit_Click() handler and commit the changes.

public partial class ShowAPIMPORT : Form
        DataTable APImportTable;
        SqlDataAdapter rst;
        public ShowAPIMPORT()
            APImportTable = new DataTable();
            rst = new SqlDataAdapter(query, connStr);

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.