[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Visual Studio (VB).net FOR Statement

Posted on 2007-07-26
10
Medium Priority
?
351 Views
Last Modified: 2008-03-06
In a Visual Studio (Visual Basic) 2005 app, we "pull" data from a Web Service, display it in a datagrid and then insert it into a SQLCE DB.

We have several FOR statements which iterate properly, we take each value from the FORs and pass them to the DB via the datagrid.

The following FOR however does get the values (maximum of three per service) but always ONLY gives us the last one. We assume that means the other two are being over-written and it isn't "stopping" until the last possible value is obtained.

The FOR is as follows (ShipToLocation is a String Collection, .Item is the DataRow, IntShipLoc1 is the SQLCE):

For Each iss As InternationalShippingServiceOptionsType In _ item.ShippingDetails.InternationalShippingServiceOption
If Not iss.ShipToLocation Is Nothing Then
.Item("IntShipLoc1") = String.Join(", ", iss.ShipToLocation.ToArray())
Else
.Item("IntShipLoc1") = "False"
End If
'Next

The FOR statements that do work have a count as follows (Minus 1 to Minus 3 across three FORs :

For int1 = 0 To item.ShippingDetails.InternationalShippingServiceOption.Count - 1

We have tried "tying" the non-working FOR into the count FOR but that doesn't help either.

Any help would be hugely appreciated, we have spent days going round and round in circles !

0
Comment
Question by:comsult
  • 4
  • 4
  • 2
10 Comments
 
LVL 10

Expert Comment

by:tpwells
ID: 19573607
The two methods you have tried for the FOR would give you all that are in the collection. So it would seem that the collection has only one object in it or at least this call "_ item.ShippingDetails.InternationalShippingServiceO" is returning only one object. check that call to make sure it is returning all that you expect it to.
0
 
LVL 10

Expert Comment

by:tpwells
ID: 19573627
It also looks like you are wanting this line to concat a string:

.Item("IntShipLoc1") = String.Join(", ", iss.ShipToLocation.ToArray())

if so then it should look like this:

.Item("IntShipLoc1") &= String.Join(", ", iss.ShipToLocation.ToArray())

Or use a stringbuilder to build the string then after the FOR assign the stringbuilder.ToString() to .Item("IntShipLoc1").
0
 

Author Comment

by:comsult
ID: 19573645
Thank you for the comment.

We have an XML log file which comes from the Web Service as a return to our initial call. It shows that we are definitely getting the three values. (Value 1 is Europe, Value 2 is US, Value 3 is Asia). Our FOR only ever gives us the third value Asia in the datagrid.

Is there a way to reference each individual value in a FOR as you can in an array ?

I should also say that we have output the call to Console and we do see all the values, hence thinking the FOR is at fault.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:wsh2
ID: 19573677
Regardless, of how many times you loop, your code is only updating ONE variable object..
    .Item("IntShipLoc1")

(As you have not specified the With statement I can't say whose object is getting updated)

Perhaps this should read something like:

    .Item("IntShipLoc" & iss.LocationNumber.ToString)

or

    .Item("IntShipLoc" & MyCounter.ToString)

but without seeing the With clause.. I am only guessing.
0
 
LVL 10

Expert Comment

by:tpwells
ID: 19573717
wsh2 is right.

My second post shows how you would be able to get them on into the one column that is being assign through concating as strings.

What columns should each of the locations be assigned to or are they all supposed to go into the same column?
0
 

Author Comment

by:comsult
ID: 19573808
Thank you both for the help.

There are three columns, IntShipLoc1, IntShipLoc2 and IntShipLoc3.

Theoretically, each column could have more than one value (IntShipLoc1 could be Europe and European Union for example), hence the string concatenation.

As to the WITH, we have one at the start of our DataGrid / DataRow

With ItemRow
Data
Data
Data
End With

In the FOR we have that works : Count -1, Count -2, Count -3 we then specifiy .Item(Variable1), .Item(Variable2), .Item(Variable3). Variable 1 is for Count-1 etc.

In the FOR that doesn't work, we don't seem to be able to use a Count but the other FORs don't have string collections, nor do they concatenate.
0
 
LVL 10

Assisted Solution

by:tpwells
tpwells earned 400 total points
ID: 19573857
How many IntShipLoc columns are there and how many possible locations are there? How will you determine which ones will need to be concated together?

As far as a Counter you can create a one just before entering the loop.

Dim ShippingCounter as Intger = 1
For Each iss As InternationalShippingServiceOptionsType In _ item.ShippingDetails.InternationalShippingServiceOption
If Not iss.ShipToLocation Is Nothing Then
.Item("IntShipLoc" & ShippingCounter.ToString()) = String.Join(", ", iss.ShipToLocation.ToArray())
Else
.Item("IntShipLoc1" & ShippingCounter.ToString()) = "False"
End If
ShippingCounter += 1
'Next
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 1600 total points
ID: 19573885
How about this then..
----------------------------------------------------------------------------
'  Initialize
   .Item("IntShipLoc1") = "False"
   .Item("IntShipLoc2") = "False"
   .Item("IntShipLoc3") = "False"
   Dim IntCount As Integer = 1
   For Each iss As InternationalShippingServiceOptionsType In _ item.ShippingDetails.InternationalShippingServiceOption
      If Not iss.ShipToLocation Is Nothing Then
         .Item("IntShipLoc" & intCount.ToString) = String.Join(", ", iss.ShipToLocation.ToArray())
         intCount += 1
         If intCount > 3 Then Exit For
      End If
   Next
----------------------------------------------------------------------------
0
 

Author Comment

by:comsult
ID: 19573917
Thank you both, I'm just going to try your suggestions.

One thing, changing to

 .Item("IntShipLoc1") &= String.Join(", ", iss.ShipToLocation.ToArray())

gave all our values in the one column which is the first time we have seen everything at once (outwith the XML log of course).
0
 

Author Comment

by:comsult
ID: 19573981
I really can't thank you both enough, wsh2's code works perfectly.

I have split the points as I can also use parts of the other suggestions.

Again, thank you both, sanity is restored !!!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

825 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