string collection? to dynamic list of db fields?

Posted on 2006-05-05
Last Modified: 2010-04-23
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?
Question by:schraudog
    LVL 34

    Accepted 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"


    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    Suggested Solutions

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    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…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    794 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

    16 Experts available now in Live!

    Get 1:1 Help Now