Solved

First person to solve this wins 500 points

Posted on 2006-07-14
11
366 Views
Last Modified: 2008-01-09
For i = 0 To NewProgram.ItemArray.Length - 1
         If Not IsDBNull(CurrentProgram.ItemArray(i)) Then NewProgram.ItemArray.SetValue(CurrentProgram.ItemArray(i), i)
Next
         
Why does this NOT update the newprogram.ItemArray values?

The newprogram and current program are both strong typed datarows.  They have over 75 Fields, and it would be soooooo much nicer to copy using the correct method.  Cheers.

PS I have stepped in the debugger and I have definately confirmed that when the value is not null, it does not copy (ie executes the line after the then) and each item in teh newprogram.itemarray is null.

GRRRR
0
Comment
Question by:spinalogic
11 Comments
 

Author Comment

by:spinalogic
ID: 17106427
let me clarify the debugging part

The line after the then DOES execute.

However the newprogram.itemarray(i) value remains as null.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17106464
Hi spinalogic,
----------

looking at your code this is VB instead of C#
but for the problem, i've no idea because its a bit difficult to debug but you can try to clone the object into the new array like

For i = 0 To NewProgram.ItemArray.Length - 1
  If Not IsDBNull(CurrentProgram.ItemArray(i)) Then NewProgram.ItemArray.SetValue(CurrentProgram.ItemArray(i).Clone(), i)
Next

and be sure the newprogram array is initialized but i don't think that is the problem becuase you get no warning or other message in the error window ...

----------
bruintje
0
 

Author Comment

by:spinalogic
ID: 17106475
The object is created using the newstrongtypedrow function (newPatientsCareProgramrow) function

I'll try clone
Egad, I don't know how VB objects work, I'm a java programmer by trade. I'd be happy with C# but I work for money and I do what I'm told.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:spinalogic
ID: 17106486
Clone isn't a public member of most of the types out of the datagrid (Guid in particular)

So what else can we try.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17106503
are you trying to copy data grid values from one datagrid to another?
0
 

Author Comment

by:spinalogic
ID: 17106515
no.

I have a strong typed data row that I wish to copy to a NEW datarow, and then modify some value on the NEW one.
IE Find a row
Copy row
Change its PK and relevant fields
Update DB

I was using that iterator to copy each column. But that doesn't update a single field on the newprogram row.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17106645
if i understand the benefit of the strongly typed datarow you can alos try to copy the values directly from the one to the other like

If Not IsDBNull(CurrentProgram.ItemArray(i)) Then
    with NewProgram.ItemArray(i)
        .PK = CurrentProgram.ItemArray(i).PK
        .relevantfields = CurrentProgram.ItemArray(i).relevantfields
    end with
end if
0
 
LVL 5

Accepted Solution

by:
pgloor earned 500 total points
ID: 17106833
Try to assign the data to DataRow.Item( index ) instead of using DataRow.ItemArray.SetValue

For i = 0 To NewProgram.ItemArray.Length - 1
         If Not IsDBNull(CurrentProgram.ItemArray(i)) Then NewProgram.Item(i) = CurrentProgram.Item(i)
Next

Not sure this is the correct way to do but it might work.

If you are just looking for a way to copy (clone) datarows from one table to another you might also read this article in the knowledge base (C#):

http://support.microsoft.com/default.aspx?scid=kb;en-us;q308909
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17108223
There is a distinction between null and DBNull, so you have to be clear about that.  Which is it?  What type is stored in CurrentProgram?

Bob
0
 

Author Comment

by:spinalogic
ID: 17112433
The values on the new Row start as DBNull.value and remain DBNull.Value after the .setvalue command.

Bruintje is very correct. HOWEVER there are over 75 columns on this datarow, and each has to be checked for nulls, and very tedious set of typing for every single name.  So although what you said is valid, its not quite what I'm after. And that c# "with" operator looks v cool :D. Never seen it before.

I couldn't obtain the count of the item collection, and I don't know whether its 75 or what else was in it.  I'll give your suggestion a go pgloor and then I'll go from there.
0
 

Author Comment

by:spinalogic
ID: 17116114
Worked first time.

Thats very weird that the itemarray doesn't work though?  Perhaps I have misunderstood their object model, but there was no count variable on the row object, so it was impossible to directly derive how many columns there were without using it.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Login 17 48
C# Single Form 8 42
Reset asp.net login password 4 42
In SQL Server 2016, can we configure MASKing rules that applies for application level users ? 12 55
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now