Darren
Darren

Reputation: 1

Why does Dataset.GetChanges(DataRowState.Modified) return null?

I would like to ask regarding C# .Net.

Why does my _DataSet.GetChanges(DataRowState.Modified) return a null value?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace test_for_error
{
    public partial class Form1 : Form
    {
        private SqlDataAdapter _DataAdapter;
        private DataSet _DataSet;
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(@"Data Source=BEB7WILLOW\BEB7WILLOW; 
                                                   Database=Cost_Estimate_DB; 
                                                   Trusted_Connection=No; 
                                                   User ID=sa; 
                                                   password=s@password1");

            _DataAdapter = new SqlDataAdapter("select client_name from cost_estimate", cn);

            _DataSet = new DataSet();
            _DataAdapter.Fill(_DataSet, "cost_estimate");
            this.txtClient.DataBindings.Add("Text", _DataSet, "cost_estimate.client_name", true, DataSourceUpdateMode.OnPropertyChanged);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _DataSet.AcceptChanges();
            DataSet DatasetChanges = _DataSet.GetChanges(DataRowState.Modified);          
        }
    }
}

Upvotes: 0

Views: 5454

Answers (3)

Darren
Darren

Reputation: 1

@Sean

I did what you said and it works. But I have another concern, every time I extract the subset using this code

if (_Ds.HasChanges())
{
   DataSet DsChanges = _Ds.GetChanges(DataRowState.Modified);
}

when I checked the data in my new dataset DsChanges the data that is extracted was still the original data upon loading of form? It should be the changes that I made in _Ds. I ended up using stored procedure and it was not the elegant way of solving my problem.

Upvotes: 0

Andreas
Andreas

Reputation: 1

Give this a try

private void btnSave_Click(object sender, EventArgs e)
{            
   DataSet DatasetChanges = _DataSet.GetChanges(); 
   _DataSet.AcceptChanges();         
}

Upvotes: 0

Sean Fausett
Sean Fausett

Reputation: 3730

You're calling DataSet.AcceptChanges before DataSet.GetChanges, which is resetting the RowState so nothing appears to be modified.

"The RowState property of each DataRow also changes; Added and Modified rows become Unchanged, and Deleted rows are removed."

Upvotes: 2

Related Questions