Cannot Update DataRow in DataSet

L

localhost

Inside the ASP.NET code below, I dig a small DataSet out of Session,
make a change, and set a VS.NET breakpoint afterwards to see via
QuickWatch if the row has changed. It has not changed. What do I
have to fix in my code to make the DataRow inside the DataTable inside
the DataSet save?

dataSet.Tables["Data1"].Rows[idValue].ItemArray[1] =
textbox.Text.Trim();
dataSet.Tables["Data1"].Rows[idValue].ItemArray[2] =
dropdown.SelectedItem.Text;
dataSet.Tables["Data1"].Rows[idValue].ItemArray[6] =
Context.User.Identity.Name.ToLower().Trim();
dataSet.Tables["Data1"].Rows[idValue].ItemArray[7] =
System.DateTime.Now;
dataSet.Tables["Data1"].AcceptChanges();
dataSet.AcceptChanges();

Thanks.
 
K

Kevin Yu [MSFT]

Thanks for Miha's quick response.

Hi,

Thank you for using MSDN Newsgroup! My name is Kevin, and I will be
assisting you on this issue.

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you didn't get any changes after
modifying some fields. If there's any misunderstanding, please feel free to
point me out.

Based on my research, just as Miha mentioned you have called AcceptChanges
method, so the changes will not be saved. Each DataRow object has a
RowState Property. The value is one of the DataRowState enumerations. When
the data in the row was not changed, the RowState remains to be
DataRowState.Unchanged. If the row was a new one, the RowSatate will be
DataRowState.Added. The rest may be deduced by analogy. The DataAdapter
will call different commands according to the RowState. If the RowState is
Added, it will call InsertCommand. If the RowState is Modified, it will
call UpdateCommand.

However, when you call AcceptChanges on a DataTable or DataSet, it will set
all the RowState property of the underlying rows to Unchanged. Thus, none
of the rows will be updated because the DataAdapter cannot find any changed
rows. So please try to remove the last two lines of codes which calls
AcceptChanges method.

For more information about AcceptChanges, please refer to:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatarowclassacceptchangestopic.asp

If you're interested in RowState property, please check:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatarowclassrowstatetopic.asp

Does this answer your question? 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."
 
L

localhost

using System;
using System.Data;

/*
I have extracted the code from my ASP.NET stuff to run
inside a console application to illustrate my problem.

Please note that I am *not* using a DataAdapter at all,
as I am not connecting to any database. I have also
not used the AcceptChanges() method.

I am simply making a dataset from scratch,
making a datatable from scratch, adding some rows, and then
modifying a datarow.

No exception occurs when I make the
data change, but the data is not actually changing either.

I think I am missing a command to make the data
edit "stick", but I cannot tell what it is.

Help!

*/

public class DataRowUpdate
{
[STAThread]
static void Main(string[] args)
{
DataSet dataSet = new DataSet("dataSet" );
DataTable dataTable = new DataTable( "dataTable" );
dataTable.Columns.Add( "iID", typeof(System.Int32)
).AutoIncrement = true;
dataTable.PrimaryKey = new
DataColumn[]{dataTable.Columns["iID"]};
dataTable.Columns.Add( "Value1" , typeof(System.String) );
dataTable.Columns.Add( "Value2" , typeof(System.String) );
dataSet.Tables.Add( dataTable );

dataSet.Tables[0].Rows.Add
( new Object[]
{
null ,
@"domain\user1" ,
"static"
}
);
dataSet.Tables[0].Rows.Add
( new Object[]
{
null ,
@"domain\user2" ,
"dynamic"
}
);

Console.WriteLine(
dataSet.Tables["dataTable"].Rows.Count.ToString() );
dataSet.WriteXml("test1.xml" ,
System.Data.XmlWriteMode.WriteSchema );
Console.ReadLine();
// Note in debugger what row #1 looks like

DataRow[] dataRows = dataSet.Tables["dataTable"].Select(
"iID=2" );
dataSet.Tables["dataTable"].Rows[1].ItemArray[1] =
"willnotsave";
dataSet.Tables["dataTable"].Rows[1].ItemArray[2] =
"pleasehelp";
Console.ReadLine();
// Note in debugger what row #1 looks like, it has not changed
dataSet.WriteXml("test2.xml" ,
System.Data.XmlWriteMode.WriteSchema);
}
}
 
G

Guest

Things work if the ItemArray is not directly addressed; just address the DataTable location directly like this

dataSet.Tables["dataTable"].Rows[1][2] = "somevalue
 
K

Kevin Yu [MSFT]

Hi,

Assigning values to an item of the DataRow.ItemArray doesn't work, because
when you get the DataRow.ItemArray property, it will create a new array of
objects, whose value are the same as those stored in the DataRow. So when
you're assigning value to dataSet.Tables["dataTable"].Rows[1].ItemArray[1],
you're actually assign value to another array, not the value contained in
the DataRow. Here I've two workarounds:

1. Use the Item property instead. It is the default property for DataRow.

dataSet.Tables["dataTable"].Rows[1][1] = "willnotsave";

The following link provides more information about DataRow.Item property:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatarowclassitemtopic.asp

2. Re-set the whole ItemArray property:

dataSet.Tables["dataTable"].Rows[1].ItemArray = new
Object[]{null,"willnotsave","pleasehelp"};

For more information about ItemArray property, please refer to:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatarowclassitemarraytopic.asp

Does this answer your question? 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."
 

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