DataRowVersion.Original and RowState quesions.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm fairly new to ado.net, using SQL Server stored procedures, asp.net, and
all that entails, so I'm sure I'm missing something simple and fundamental.
Hopefully you can point it out to me.
Ultimately the problem is I am getting an error: "There is no Original data
to access." when trying to execute the following line:
if (drCur["Contact", DataRowVersion.Original].ToString() != "")
this.sqldaAddSite.UpdateCommand.Parameters["@Original_Contact"].Value =
drCur["Contact", DataRowVersion.Original].ToString();

You ask a lot of questions as to why I'm doing this in the first place that
I'm not sure I can answer and I'm sure down the road I'll find better
methods. Still...

The webform that is having this problem is almost identical in logic to one
that works just fine. One difference I have noticed is that when I execute
these 3 lines:
this.sqldaAddSite.SelectCommand.Parameters["@SiteID"].Value = (string)
Session["SiteID"];
this.sqldaAddSite.Fill(this.dsAddSite1);
drCur = this.dsAddSite1.scs_GetSiteUpdate.Rows[0];
The rowstate of drCur is "Added" while on the webform that works it's
"Unchanged". Both are using store procedures for select commands.

Can anybody give me clues as to what's going on and what to do about it?
 
Hi,

Jon in Canby Or. said:
I'm fairly new to ado.net, using SQL Server stored procedures, asp.net,
and
all that entails, so I'm sure I'm missing something simple and
fundamental.
Hopefully you can point it out to me.
Ultimately the problem is I am getting an error: "There is no Original
data
to access." when trying to execute the following line:
if (drCur["Contact", DataRowVersion.Original].ToString() != "")
this.sqldaAddSite.UpdateCommand.Parameters["@Original_Contact"].Value =
drCur["Contact", DataRowVersion.Original].ToString();

You ask a lot of questions as to why I'm doing this in the first place
that
I'm not sure I can answer and I'm sure down the road I'll find better
methods. Still...

The webform that is having this problem is almost identical in logic to
one
that works just fine. One difference I have noticed is that when I execute
these 3 lines:
this.sqldaAddSite.SelectCommand.Parameters["@SiteID"].Value = (string)
Session["SiteID"];
this.sqldaAddSite.Fill(this.dsAddSite1);
drCur = this.dsAddSite1.scs_GetSiteUpdate.Rows[0];

The rowstate of drCur is "Added" while on the webform that works it's
"Unchanged".

This would definitely be the cause for the Exception you're getting. The
RowState determines how many values each field in a row has.

RowState.Added => only new data (DataRowVersion.Current)
RowState.Unmodified => old data (DataRowVersion.Original) & new data
(DataRowVersion.Current) (old=new though)

So if the RowState is Added it won't have original data, so the question is
why is the RowState Added and is it expected or not. This depends on how
this DataRow got into the DataTable.

DataRow dr = someTable.NewRow(); // RowState = Detached
dr["somefield"] = somevalue;
someTable.Rows.Add(dr); // RowState = Added
dr.AcceptChanges(); // RowState = Unmodified

DataAdapter.Fill will use the same sequence as above (for each fetched
record), making all loaded DataRows (initially) having an Unmodified state,
but this is by default. If you set DataAdapter.AcceptChangesDuringFill to
false then all DataRows will keep their Added state.

AcceptChanges():
- if the DataRow has RowState.Added then it will become RowState.Unmodified,
copying new data to old data (index copy only);
- AcceptChanges is _by default_ called by both DataAdapter.Fill and
DataAdapter.Update respectively for each loaded row and each succesfully
updated row;
- calling AcceptChanges at the wrong time may cause false concurrency
violations if you use optimistic concurrency control and use
DataAdapter.Update.

If you have set DataAdapter.AcceptChangesDuringFill to false, then set it
back to true.

HTH,
Greetings
 
Back
Top