473,586 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add New Blank Row Into Existing DataTable While Looping Problem.

Hi All,

I wrote a program insert new row between rows while the datatable is
looping. It display the error message.
Exception Details: System.Argument Exception: This row already belongs
to this table.
[ArgumentExcepti on: This row already belongs to this table.]
System.Data.Dat aTable.InsertRo w(DataRow row, Int32 proposedID,
Int32 pos) +319
System.Data.Dat aRowCollection. Add(DataRow row) +14
ASP.Outstandrpt _aspx.CheckOrde r(Object src, EventArgs e) in
c:\Inetpub\wwwr oot\Outstandrpt .aspx:398
ASP.Outstandrpt _aspx.DetailGri d_setPage(Objec t src,
DataGridPageCha ngedEventArgs e) in
c:\Inetpub\wwwr oot\Outstandrpt .aspx:118
System.Web.UI.W ebControls.Data Grid.OnPageInde xChanged(DataGr idPageChangedEv entArgs
e) +111
System.Web.UI.W ebControls.Data Grid.OnBubbleEv ent(Object source,
EventArgs e) +395
System.Web.UI.C ontrol.RaiseBub bleEvent(Object source, EventArgs
args) +31
System.Web.UI.W ebControls.Data GridItem.OnBubb leEvent(Object source,
EventArgs e) +120
System.Web.UI.C ontrol.RaiseBub bleEvent(Object source, EventArgs
args) +31
System.Web.UI.W ebControls.Link Button.OnComman d(CommandEventA rgs e)
+122
System.Web.UI.W ebControls.Link Button.RaisePos tBackEvent(Stri ng
eventArgument) +288
System.Web.UI.W ebControls.Link Button.System.W eb.UI.IPostBack EventHandler.Ra isePostBackEven t(String
eventArgument) +5
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
+166
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint)
+5157

my code:

y = myTable.Rows.Co unt;

DataRow dr = myTable.NewRow( );
DataRow mydr = myTable.NewRow( );

for (i=0; y > i ; i++ )
{
DataRow dataRow = myTable.Rows[i];
// DataRow dataRow = myTable.NewRow( );
if (myOrderNo == "")
{
myOrderNo = Convert.ToStrin g(dataRow["OrderNo"]);
}
else
{
if (y == i+1)
{
dataRow["CustNo"] = "Sub Total :";
dataRow["HDelivSeq"] = "";
myTable.Rows.Ad d(dataRow);
}
}

if (myOrderNo != Convert.ToStrin g(dataRow["OrderNo"]))
{
dataRow["CustNo"] = "Sub Total :";
dataRow["HDelivSeq"] = "";
dataRow["OrderNo"] = "";
myTable.Rows.Ad d(dataRow);
}
}
DetailGrid.Data Source = myTable;
Nov 16 '05 #1
1 22535
Here's the problem: You are creating the dataRow object and setting it
equal to a row that already exists right here:
DataRow dataRow = myTable.Rows[i];
And then you are attempting to add the row that already exists here:
myTable.Rows.Ad d(dataRow);
What you should be doing is using one of these variables that you've
created but not used:
DataRow dr = myTable.NewRow( );
DataRow mydr = myTable.NewRow( );


So in your loop, you need to do this:

DataRow dr = myTable.NewRow( );
// do any processing here to the datarow
myTable.Rows.Ad d(dr);

The whole point of using NewRow() is to create a new row object that has
the same schema as the table. But you have to add that same object to
the table.

Lowell

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6314
by: Shelby | last post by:
Problem: My company generates its own data export from a propietary database. These (free) tables can be read in C#.NET using a Visual FoxPro driver (vfpoledb). I can read each of the six tables into its own datatable, modify them, and add them to a dataset. It take approximately 15 minutes to pass that dataset to Crystal Reports (45...
1
25516
by: Kelvin | last post by:
Hi All, How to get column value from datarow while datatable looping ? Please advised !
4
34595
by: Kelvin | last post by:
Dear All, Please help. Same as subject !!!
1
2198
by: Kelvin | last post by:
Hi All, While the existing datatable is looping, I can insert new row into datatable, but I can't insert it between datarows. All new rows will inserted into buttom row. Please advise!
2
1732
by: Mark Perona | last post by:
I created an ASP.net form with an editable datagrid on it. I can create new records, and update and delete existing records. The problem I have is that I want a field in the grid to be editible when I'm editing a new record, but disabled when editable an existing record. Any ideas Mark
5
3365
by: Stimp | last post by:
This is a question I'm carrying over from a previous one I made today since I've simplified where the problem is... I have a datatable, tblFeatures, which has around 30 columns (one for each 'feature'). I also have between 1 and 3 rows of data (one for each 'vehicle'). I want to transpose this table so that I ouput the rows horizontally...
2
2462
by: Roy | last post by:
Hi all, I do have a datatable that looks like: id Number Description 1 1 Desc1 2 1 Desc2 3 2 Desc3 I need this datatable looks like (with 4 rows which the third one is blank): id Number Description 1 1 Desc1 2 1 Desc2
7
6598
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer include them in the set that is displayed to the user) but I don't want to delete the corresponding row from the database. I've tried using the...
13
2586
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I want to create a new column in a datatable from two existing columns. I have no problem to create the new column using the datatable.columns.add method. The problem is the value of the new column may become system.dbnull since one of the two existing columns may have system.dbnull. How can I fix it so the new column will get the value of...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.