0

i would like some help with this, i have tried searching on google and have tried searching here with no luck.

what i have tried so far is the following:-

For Each value As Configuration.SettingsPropertyValue In My.Settings.PropertyValues

    Dim myrecord As String = My.Settings.PropertyValues.ToString

    sql_all_matching_records = String.Format("")

Next

enter image description here but this just gets skipped when i run in debugger mode and have put a break point within the for loop.

what i want to do is be able to loop through the variable names in my.settings and then take its value and compare it to an SQL lookup. the only thing i am struggling with is looping through the my.settings.

EDIT1: ABOVE has been answered, however getting the error in picture, have a wrote the code wrong? not sure i understand what vb.net is trying to tell me. i thought it was correct.

Danny James
  • 244
  • 3
  • 16
  • 1
    I was able to reproduce your problem, weird solution but if you access a value from your settings before the loop (i.e. Dim dummyString As String = My.Settings.aSettingName) then it seems to work. This feels like a situation where the settings haven't been loaded yet, but I must admit I'm not too familiar with how/when settings are initialized. But adding that line in front of the loop and grabbing the value of a "dummy" setting seemed to fill the PropertyValues collection and allow to the loop to execute – soohoonigan Jul 28 '17 at 17:28
  • wow - good answer - it worked but now it has given me a new problem, i will edit question, then post the above as answer as it is right and will put as answer. – Danny James Jul 29 '17 at 12:59

4 Answers4

2

A bit of a disclaimer, I'm not familiar with how/when settings are loaded and the PropertyValues collection gets filled, but it doesn't seem to hold anything until a settings value is accessed. It's a wierd workaround, but grabbing a value from settings before looping through the collection seems to fill it. Your InvalidCast exception occurs because you're trying to set myrecord = a collection.tostring. We already know that the collection has multiple values because we're iterating them, so we can't just call .tostring on it. That's like saying Array.ToString(), just doesn't work that way. I think what you're actually looking for is value.PropertyValue.ToString, that will hold the settings value you're trying to get. With both those changes applied, you get this:

    Dim dummy As String = My.Settings.dummySetting
    For Each value As System.Configuration.SettingsPropertyValue In My.Settings.PropertyValues

        'Dim myrecord As String = My.Settings.PropertyValues.ToString
        Dim myrecord As String = value.PropertyValue.ToString

        sql_all_matching_records = String.Format("")
    Next

The dummySetting is just an empty value that I put in settings to call on. Hope this helps

soohoonigan
  • 2,342
  • 2
  • 10
  • 18
0

I think your line #2 is wrong...

For Each value As Configuration.SettingsPropertyValue In My.Settings.PropertyValues

    Dim myrecord As String = value.ToString

    sql_all_matching_records = String.Format("")

Next

I have not tried your code but I think what You are doing in your line 3 is taking all of the properties of each setting and concatenating them into one.

Edited to point at correct line #2 not #3

Zeddy
  • 2,079
  • 1
  • 15
  • 23
  • Hi, as said in the answers it doesn't even get to line 2 in the debugger, i put a break on line 2 but never breaks just skips the whole loop. – Danny James Jul 29 '17 at 12:53
0

Some storage areas do not know where they begin or end! When you send the cursor to the top, it travels upward until it runs out of records and stops with the first record just below the cursor. Your first command must be down to reach the first record.

mac318
  • 1
  • 1
0

If looking for a case-insensitive lookup, you may do something like this:

        For Each AProperty As Configuration.SettingsPropertyValue In My.Settings.PropertyValues
            If AProperty.Name.ToLower = ("ExPectedName").ToLower Then
                ReturnValue = AProperty.PropertyValue
                Exit For
            End If
        Next
PaulvdElst
  • 54
  • 6