DataRowView exception

S

Steve Loper

I can't find this documented anywhere, but it appears that you cannot
set the value of a field in a DataRowView more than once without
calling AcceptChanges or RejectChanges in the underlying DataTable.
Can anyone confirm this? Is this documented anywhere?

The following C# code snippet throws an exception on the last line:

DataRowView drv = (DataRowView)
this.BindingContext[myDataView].Current;
drv["NAME"] = "a";
drv["NAME"] = "b";

The following code works fine:

DataRowView drv = (DataRowView)
this.BindingContext[myDataView].Current;
drv["NAME"] = "a";
myDataView.Table.AcceptChanges();
drv["NAME"] = "b";
myDataView.Table.AcceptChanges();


The exception thrown in the fist case is as follows:

A first chance exception of type 'System.Data.DataException'
occurred in system.data.dll

Additional information: Cannot set NAME.

Thanks in advance,

Steve Loper
(e-mail address removed)
 
G

Greg

My first thought is you have already set a Current Version and you cannot
set 'another' Current Version. Makes sense if you think about it. If you
are databinding, just do this through the control.
 
R

Ravi[MSFT]

This should not happen. The following for example does not throw any
exception.If you've a particular scenario where you are seeing the
exception, it'd be great if you can attach a repro case.
---
using System;
using System.Data;

class Test {
public static void Main()
{
try {
DataSet ds = new DataSet();
DataTable dtCust = ds.Tables.Add("Customers");
DataColumn c =dtCust.Columns.Add("CustId", typeof(string));
dtCust.Columns.Add("Name", typeof(string));
dtCust.Columns.Add("Age", typeof(int));
dtCust.PrimaryKey = new DataColumn[] { c };

dtCust.Rows.Add(new object[] { "A", "foo", 25 });
dtCust.Rows.Add(new object[] { "B", "bar", 30 });
dtCust.Rows.Add(new object[] { "C", "foo", 35 });

ds.AcceptChanges();

DataView dv = new DataView(dtCust);
dv[0]["Name"] = "bar1";
dv[0]["Name"] = "bar2";
} catch (Exception e) {
Console.WriteLine(e);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top