Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Copying data into an existing datarow
Message
From
04/04/2007 13:45:31
 
 
To
04/04/2007 12:47:14
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
01211623
Message ID:
01212118
Views:
15
>If I translate that wacky C# correctly, then I forgot all about that. I must spend too much time gabbing in the chatter forum...

LOL!!

Your post actually didn't have any code ... you just suggested using the .Merge() and I took it from there for the Tip.

But you are entirely too chatty in the chatter forum. <g>

~~Bonnie





>
>>Paul,
>>
>>Have you tried using DataSet or DataTable .Merge()? Check out the second Tip from my Tips column in the current .NET Magazine here:
>>
>>http://www.levelextreme.net/ViewPageArticle.aspx?Session=6F45495A76536D74527A303D203255557039742B6A2B746955644A3873484A793370773D3D
>>
>>Well, actually, let me just copy it here (it's funny because this tip was originally inspired by a post from Mike Cole ... I guess he doesn't remember his own posts <g> ... I elaborated on it a bit for my tip column though):
>>
>>If you want to create a new DataTable with data from an existing DataTable, you can always use the DataTable.Copy() method. But, what if you want to copy that data (or even a subset of that data) to another existing DataTable? Here's an easy trick:
>>
>>
>>DataRow[] rows;
>>
>>// To copy all the rows
>>rows = MyOldTable.Select();
>>MyDataSet.Merge(rows);
>>
>>// To copy a subset
>>string FilterSubset = "code = '4'";
>>rows = MyOldTable.Select(FilterSubset);
>>MyDataSet.Merge(rows);
>>
>>
>>Unfortunately, there is one drawback to this approach ... this signature of the .Merge() method is only supported by DataSet (in other words, you can't use the DataTable.Merge() method).
>>But, if you're using Visual Studio 2005 ... then no worries mate! There's the new .ToTable() method to the rescue!
>>
>>
>>DataTable dtCopy = MyOriginalTable.DefaultView.ToTable();
>>MyNewTable.Merge(dtCopy);
>>
>>
>>
>>~~Bonnie
>>
>>
>>
>>>>Paul... you said Cursor, do you mean datatable?
>>>
>>>Yeah, both of them are datatable (I was justing using two different terms to help differenciate between them in my post).
>>>
>>>I think I've got this resolved, but if there is a built-in way I'd love to hear it. I'm essentially just iterating through the columns of the SQL DataTable and grabbing the column name, then checking to see if there is a matching column in my XML DataTable. If there is, I copy the value over.
>>>
>>>I'm also not entirely sure why SetModified() is required - I would have expected this to get set as soon as I modified the value.
>>>
>>>
>>>drMatch[0].SetModified();
>>>
>>>foreach (DataColumn column in dtPortal.Columns)
>>>{
>>>    DataColumn xmlColumn = dt.Columns[column.ColumnName];
>>>    if (xmlColumn != null)
>>>    {
>>>        drMatch[0][column.Ordinal] = row[xmlColumn.Ordinal];
>>>    }
>>>}
>>>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform