Exception when setting column values through a data row view

G

Guest

I need help with a strange problem occuring when trying to set values in a DataRowView

To put the problem in context

I am hooking up to the ListChanged event of a DataView. When a new row is added I have a method called from the event handler to populate every row in the DataView with some data, column by column. I am trying to set a value in several of the columns (eg dataView[0][21] = <my new value>). This works for the first column that I set, but on the next column it throws an exception "Object reference not set to an instance of an object"

This problem mostly seems to happen on the first row being added, although it does occasionaly happen at other times

Is this a bug, or am I doing something wrong?
 
K

Kevin Yu [MSFT]

Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that an exception is thrown when you are
trying to set values for a column. If there is any misunderstanding, please
feel free to let me know.

Generally this exception might be caused by an incorrect column index.
Would you please set a break point on the line which throws the exception.
If dataView[1][21] = <my new value> throws the exception. Please check
values for dataView[1] and dataView[1][21] in the watch windows. This can
help us find which reference didn't point to a valid object. If you cannot
see the watch windows, please show it from Debug / Windows / Watch.

If the problem still persists, could you please show us your code?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

I have checked that the column indicies are correct, so that is not the problem. What seems to be happening is that for some reason the row is removed from the DataView as soon as one of its column values is set. Could this be something to do with the RowStateFilter value?
 
K

Kevin Yu [MSFT]

Hi,

This issue might have something to do with the RowFilter and
RowStateFilter. For example, if you are filtering the all the unchanged
rows with your DataView, and you made some changes to a row in it, this row
will be removed from the DataView. Please check your code context to see if
the DataView is has RowFilter or RowStateFilter set. If the problem still
persists, could you show us your code, so that we can deliver our
assistance more quickly?

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

RowStateFilter was set to Current (or possibly it was Current | Added). RowFilter was set to "". I have found a different way of doing things now, but it would be nice to get to the bottom of the problem

Sam
 
M

[MSFT]

Hi Sam,

It is better to post some detail code you used in ListChanged event so that
we can narrow down the problem and make sure what happened.

Regards,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Sorry if this gets posted twice

I have created some example code (different to my actual code) but it exhibits the same problem. A comment line shows where the exception occurs. Hope this helps.

class Class

DataSet dataSet
DataView dataView
/// <summary
/// The main entry point for the application
/// </summary
[STAThread
static void Main(string[] args

Class1 myClass = new Class1()

myClass.Problem()



private void Problem(

dataSet = new DataSet("My Data Set")
DataTable table = dataSet.Tables.Add("MyTable")

table.Columns.Add("RowID",typeof(System.Int32))
table.Columns["RowID"].AutoIncrement = true
table.Columns["RowID"].AutoIncrementStep = 1
table.Columns["RowID"].Unique = true

table.Columns.Add("Col1")
table.Columns.Add("Col2")
table.Columns.Add("Col3")

table.PrimaryKey = new DataColumn[] { table.Columns["RowID"] }

dataView = new DataView(dataSet.Tables[0],"","RowID ASC", DataViewRowState.CurrentRows)
dataView.ListChanged +=new ListChangedEventHandler(dataView_ListChanged)

DataRow newRow = dataSet.Tables[0].NewRow()
newRow.EndEdit()
dataSet.Tables[0].Rows.Add(newRow)

Console.ReadLine()


private void dataView_ListChanged(object sender, ListChangedEventArgs e

if (e.ListChangedType == ListChangedType.ItemAdded)

// the Lis
if (dataView[e.NewIndex].Row.RowState == DataRowState.Added)

dataSet.Tables[0].BeginLoadData()

for(int columnNum = 1; columnNum < dataSet.Tables[0].Columns.Count; columnNum++)

for (int i = 0; i < dataView.Count; i++)

try

dataView.BeginEdit()
// the following line generates an exception when columnNum is 2 and 3, but not when it is
dataView[columnNum] = 1
dataView.EndEdit()
}
catch (Exception ex)

Console.WriteLine("i = {0}, columnNum = {1}", i, columnNum)
Console.WriteLine(ex)




dataSet.Tables[0].EndLoadData()



}
 
K

Kevin Yu [MSFT]

Hi Samuel,

Thanks for the repro code. I was able to reproduce it on my machine. Based
on my research, this is a known issue. Write operations are currently not
supported on the ListChanged event handler. This is by design. So I'm
afraid that we cannot assign values to certain columns in
dataView_ListChanged.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

In that case, are you going to change the design in ADO.Net v2? I was originally wanting to set some of the values on the RowChanged (row added) event of the DataTable, but when that event is raised the row isn't actually part of the data table (contrary to what the name of the event would lead one to believe). Because of this, I tried using the ListChanged event of the DataView which does contain the row when the event is raised. And now you say the values cannot be set during that event

I'm currently having to set a timer during the RowChanged event to call back another method after the RowChanged event is complete to do my processing of the new Row. I'm sure you'll agree that this is horribly inelegant. I sincerely hope that you are able to do something about this before .Net v2 comes out

Sam
 
K

Kevin Yu [MSFT]

Hi Sam,

Thanks so much for your feedback. I'm not quite sure if this feature will
be changed in next version before it is released. If you have good
suggestions that will make our products better, please feel free to send
mail to (e-mail address removed). Your suggestions will be highly appreciated.
I will also forward this to the corresponding team. Thanks again for your
feedback!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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