Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

string collection? to dynamic list of db fields?

Please help a .net 2.0 newbie with some programming technique.

I have a table that I am inserting rows with many columns including
WARN1, WARN2... WARN8 all nvarchar(35).  I do not have control over this
database structure.

My code needs to process some logic and keep a list of WARNings to be
inserted.  One one row I might need to use 3 warning columns, on one I might
need all 8 on the next I might need only 2, etc.

I am learing how to use strongly typed datasets and have things working so far like this:

Dim shipToAdapter As New dsShipToTableAdapters.SHIPTOTableAdapter
Dim shipTos As New dsShipTo.SHIPTODataTable
Dim shipTo As dsShipTo.SHIPTORow
shipTo = shipTos.NewSHIPTORow
With shipTo
      .ID = inputRS.ID
      .NAME = inputerRS.Name
End With

In vb 6 i did the WARNx columns with a counter and a dynamic field reference like...

if isnull(inputRS.fields("SHIP_VIA")
      totalWarnings = totalWarnings + 1
      outputRS.fields("WARN" & totalWarnings) = "The shipvia vield was empty!"
end if


if something else is wrong then
      totalWarnings = totalWarnings + 1
      outputRS.fields("WARN" & totalWarnings) = "Something else is wrong"
end if

How would I accomplish this correctly using VB.Net 2.0?
1 Solution
Although I haven't tested, I don't see why you can't use a similar approach in VB.NET 2005.  

A datarow's items can be referenced by number or name.  If your various warning columns are contiguous in the row, then you can reference them by number: initally set totalWarnings to the number of the first of them, increment it as each warning is added, and the next warning will go in the next warning column.  If they are not contiguous, or you prefer to use names, then the field column name should be more or less like your example

     outputRS.fields("WARN" & totalWarnings) = "Something else is wrong"

although modified like this

     mydatarow("WARN" & totalWarnings.ToString) = "Something else is wrong"

or, if you wanted to do it within your

   With shipTo

block, like this

   .Item("WARN" & totalWarnings.ToString) = "Something else is wrong"

schraudogAuthor Commented:
Roger,  Thanks!   the With shipTo block example worked right out of the gate.  Excellent!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now