Combo box values from array generated with table value using DataTable.Rows(index)("fieldname")

Posted on 2006-04-17
Last Modified: 2010-04-23
I cannot get my combo box to populate with an array that is based on a string in a table I use.

dt is the DataTable
index is a string that contains the index number from a DataRow
array_value is the field name that contains the text     {"", "Mr.", "Mrs.", "Dr."}

       Dim arrCriteria1() As String = dt.Rows(index)("array_value")
       cboCriteria1.DataSource = arrCriteria1

Regardless of what I do I continue to get a "specified cast is not valid" error.

I can however see that dt.Rows(index)("array_value") contains an appropriate value using MsgBox.
I also took the '{}' characters out of the array_value string and set up the command as follows:

       Dim arrCriteria1() As String = {dt.Rows(index)("array_value")}

It displayed the whole string..."", "Mr.", "Mrs.", "Dr." cboCriteria1.Text
I'm assuming it has to do with a conversion.  But I am without the knowledge of it.
This is a critical form in my design.  Without this capability I'm dead in the water.

Thanks for any help!

Question by:TSFLLC
    LVL 14

    Accepted Solution

    Check the Length of your string array. It probably says 1, because the value you are providing is just a string that looks like an array.

    To make it into an array use Split like e.g.

        Dim a As String = """test"",""test2"",""test3""" ' example data like you are providing now

        Dim ar() As String = a.Split(","c) ' split the data on the comma

    You might want to replace the " with nothing later, but I let that up to your imagination

    Author Comment

    That worked like a charm!

    My arrays in this form contain a limited number of values.

    Therefore, I did remove the quotations altogether.  I also included in empty string for the first value so that no values would display in
    my newly defined combo box.

    Thanks much!
    LVL 14

    Expert Comment

    You're welcome!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    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…
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    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

    12 Experts available now in Live!

    Get 1:1 Help Now