|
-
Nov 19th, 2007, 12:51 AM
#1
Thread Starter
Just Married
[2.0] Update a datarow
Hi all
What is the best method to update a data row, here what I am doing is searching the row from the datatable , then deleting the row and after modifying the row adding the row again.
But I am getting error when I am trying to delete the row the error saying that
This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.
basically I want to update a datarow in the datatable according to the condition.
Thanks
Code is as below
C# Code:
DataTable dataTable = userTrackingProcess.PageDetail;
DataTable saveDataTable = GetStructure();
//STURCTURE OF BOTH DATATABLE ARE SAME
string lastPageName=string.Empty;
try
{
SessionPage sessionPage = new SessionPage();
foreach (DataRow dataRow in dataTable.Rows)
{
Browser = (string)userTrackingProcess.Browser.ToString();
if (!(lastPageName.Equals(string.Empty)))
{
if (!lastPageName.Equals(dataRow["Page_URL"].ToString()))
{
DataRow[] row;
row = saveDataTable.Select("Page_URL='" + lastPageName + "'");
if (row.Length >= 0)
{
saveDataTable.Rows.Remove(row[0]);
saveDataTable.AcceptChanges();
row[0]["End_Time"] = (DateTime)row[0]["End_Time"];
saveDataTable.ImportRow(dataRow);
}
saveDataTable.ImportRow(dataRow);
}
else
{
DataRow[] row;
row = saveDataTable.Select("Page_URL='" + lastPageName + "'");
if (row.Length >= 0)
{
saveDataTable.Rows.Remove(row[0]);
saveDataTable.AcceptChanges();
row[0]["Total_Hit"] = (int)row[0]["Total_Hit"] + 1;
saveDataTable.ImportRow(dataRow);
}
}
}
else
{
saveDataTable.ImportRow(dataRow);
}
lastPageName = saveDataTable.Rows[saveDataTable.Rows.Count - 1]["Page_URL"].ToString();
saveDataTable.AcceptChanges();
}
//SAVING THE DATA IN THE DATABSE
if (saveDataTable.Rows.Count > 0)
{
}
}
catch
{
throw;
}
-
Nov 19th, 2007, 03:08 AM
#2
Re: [2.0] Update a datarow
First of all, you're not deleting the row; you're removing it. Deleting a DataRow and removing it are two different things.
Second, why are you removing it and add it back if all you want to do is update it? If all you want to do is update it then that's all you should do.
Third, if you really must remove the row for some reason then the very error message you posted is already telling you what to do:
This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.
Do you want to create new data in that row? If so then do what it says and call BeginEdit.
-
Nov 19th, 2007, 04:20 AM
#3
Thread Starter
Just Married
Re: [2.0] Update a datarow
Simplly say that I want to search a data row in the datatable and if found then I want to update it.
Thanks for the reply
-
Nov 19th, 2007, 05:00 AM
#4
Re: [2.0] Update a datarow
If you want to set a field in a row then just do so. You don't have to remove it first. In fact, as you've seen, removing the row discards all the data it contains.
-
Nov 19th, 2007, 05:12 AM
#5
Thread Starter
Just Married
Re: [2.0] Update a datarow
C# Code:
DataRow[] row;
row = saveDataTable.Select("Page_URL='" + lastPageName + "'");
if (row.Length >= 0)
{
saveDataTable.Rows.Remove(row[0]);
saveDataTable.AcceptChanges();
row[0].BeginEdit();
row[0]["Total_Hit"] = row[0]["Total_Hit"].ToString() + 1;
saveDataTable.ImportRow(dataRow);
row[0].EndEdit();
}
Above code is working for me , but is there is any direct way to update the datarow.. I am removing the datarow and adding it again.
Thanks
-
Nov 19th, 2007, 07:10 AM
#6
Re: [2.0] Update a datarow
Consider this. You have an egg carton with an egg in it. You take the egg out, paint it then put it back. Now you say "I want to paint the egg again but this time without taking it out of the carton and putting it back again". What would you do? Wouldn't you just paint the egg in the egg carton without removing it first and putting it back again afterwards? Would you even have to ask?
Now, you've got a DataTable with a DataRow in it. You take the DataRow out, edit it and then put it back. Now you say "I want to edit the DataRow but this time without taking it out of the DataTable and putting it back again". What do you do?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|