Solved

First person to solve this wins 500 points

Posted on 2006-07-14
11
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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