Updating Database through edits to WPF Datagrid
<p>I have seen a ton of write ups on this and yet cannot get this to work. I have a window in my C# WPF application that, on load, successfully fills a datatable based on a pre-defineds query (altered by user selections from previous window)</p>

<p>The Fill logic is here:</p>

<pre><code>private void FillDataGrid()
    {
        string ConString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
        string CmdString = string.Empty;
        using (SqlConnection con = new SqlConnection(ConString))
        {
            CmdString = $@"select PGCL.Client_Name, PGG.Guarantee_Title, PGG.Guarantee_Desc_Org, PGGM.Target, PGG.Timing_Measurement, PGP.Month, PGP.Year, PGP.Quarter, PGP.Numerator, PGP.Denominator, PGP.Performance_Percent, PGP.Achieved_Flag, PGP.Performance_ID
                            from PGG
                                inner join PGC on PGC.Contract_ID = PGG.Contract_ID
                                inner join PGCL on PGCL.Client_ID = PGC.Contract_ID
                                inner join PGGM on PGGM.Guarantee_ID = PGG.Guarantee_ID
                                inner join PGP on PGP.Guarantee_ID = PGG.Guarantee_ID
                                Where PGP.Month = {this.monthNum.ToString()} and PGP.year = {this.yearNum.ToString()}
                                    and PGC.Active_Flag = 'y'
                                    and PGG.Department_Responsible_ID = {this.deptID.ToString()}
                                Order by Client_Name, Guarantee_Order asc";

            SqlCommand cmd = new SqlCommand(CmdString, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("PG");

            sda.Fill(dt);
            DGPG.ItemsSource = dt.DefaultView;

        }
    }
</code></pre>

<p>This portion works perfectly.</p>

<p>I have 5 columns in the PGP (aliased from above) table that I want to dynamically write values (in a few steps) to based on user input at the close of the window (with a prompt to save). What method would be recommended for doing this?</p>

<p>i.e.: if a user entered the value '5' for for Numerator on row 5 and '7' for denominator on row 10, i want the following to happen:</p>

<p>1) using the PGP.ID, insert into the PGP_History table (for historical logs of changes)
2) UPDATE PGP table values accordingly.</p>

<p>I would greatly appreciate it if someone could get me going in this direction?</p>

<p>Thank you,
Wes</p>

<h2>EDIT</h2>

<p>I think I have made some progress but am stuck on the below, now:</p>

<pre><code>       private void FillDataGrid()
    {
        string ConString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
        string CmdString = string.Empty;
        using (SqlConnection con = new SqlConnection(ConString))
        {
            CmdString = $@"select PGCL.Client_Name, PGG.Guarantee_Title, PGG.Guarantee_Desc_Org, PGGM.Target, PGG.Timing_Measurement, PGP.Month, PGP.Year, PGP.Quarter, PGP.Numerator, PGP.Denominator, PGP.Performance_Percent, PGP.Achieved_Flag, PGP.Performance_ID
                            from PGG
                                inner join PGC on PGC.Contract_ID = PGG.Contract_ID
                                inner join PGCL on PGCL.Client_ID = PGC.Contract_ID
                                inner join PGGM on PGGM.Guarantee_ID = PGG.Guarantee_ID
                                inner join PGP on PGP.Guarantee_ID = PGG.Guarantee_ID
                                Where PGP.Month = {this.monthNum.ToString()} and PGP.year = {this.yearNum.ToString()}
                                    and PGC.Active_Flag = 'y'
                                    and PGG.Department_Responsible_ID = {this.deptID.ToString()}
                                Order by Client_Name, Guarantee_Order asc";

            SqlCommand cmd = new SqlCommand(CmdString, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("PG");

            sda.Fill(dt);
            DGPerformanceGuarantees.ItemsSource = dt.DefaultView;
        }
    }

    private void Commit_Updates_Click(object sender, RoutedEventArgs e)
    {
        string ConString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
        //string CmdString = string.Empty;
        using (SqlConnection con = new SqlConnection(ConString))
        {
            //Update Logic
            SqlCommand update = new SqlCommand("UPDATE PGP SET PGP.NUMERATOR = @Numerator, DENOMINATOR = @Denominator, PERFORMANCE_PERCENT = @Percent, ACHIEVED_FLAG = '@Achieved' WHERE PERFORMANCE_ID = @Performance_ID", con);
            update.Parameters.Add(new SqlParameter("@Numerator", SqlDbType.Int, 5, "NUMERATOR"));
            update.Parameters.Add(new SqlParameter("@Denominator", SqlDbType.Int, 5, "DENOMINATOR"));
            update.Parameters.Add(new SqlParameter("@Percent", SqlDbType.Decimal, 18, "PERFORMANCE_PERCENT"));
                update.Parameters["@Percent"].Precision = 18;
                update.Parameters["@Percent"].Scale = 2;
            update.Parameters.Add(new SqlParameter("@Achieved", SqlDbType.NVarChar, 1, "ACHIEVED_FLAG"));
            update.Parameters.Add(new SqlParameter("@Performance_ID", SqlDbType.Int, 5, "Performance_ID"));

            sda.UpdateCommand = update;
            sda.Update(dt);
        }

        FillDataGrid();
    }
</code></pre>

<p>I added a button to trigger the update to occur. Obviously this does not compile because the SQL Data Adapter (sda) is does not exist in the context of the button handler.</p>

<p>What can I do to resolve this?</p>