How to commit changes when displaying datagrid in C#

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwheeler23Author Commented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.