Solved

First person to solve this wins 500 points

Posted on 2006-07-14
11
358 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

19 Experts available now in Live!

Get 1:1 Help Now