Search code examples
c#vb.netapp-configvb.net-2010

How to read the value to text box from app config


I have added key values in appconfig of vb.net

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key0" value="0" />
<add key="Key1" value="1" />
<add key="Key2" value="2" />
</appSettings>
</configuration>

I would like to loop through all the keyvalues in app config and read the value to textbox1,textbox2 and textbox3.

I have done some work on it but failed to achieve it below is what i tried.

If form1.combobox1.selecteditem = 0 then
Dim appsettings = configurationmanager.appsettings
dim result as string
for each result in appsetting
if result = appsettings("Key1") then
textbox1.text = result
else
if result = appsettings("key2") then
textbox2.text = result
end if
next

The above throwed error in if condition, could you please help me to get the solution to read the values to textbox from Appconfig file in VB.net platform.


Solution

  • I am not sure what you mean by If form1.combobox1.selecteditem = 0 then If there is a 0 in the box then If ComboBox1.SelectedText = "0" Then should work. The items in a combobox are objects. If you mean there are no selected items then If ComboBox1.SelectedIndex = -1 You were missing an End If so I added it where I thought it might belong

    If ComboBox1.SelectedText = "0" Then
                Dim appsettings = ConfigurationManager.AppSettings
                Dim result As String
                For Each result In appsettings
                    If result = appsettings("Key1") Then
                        TextBox1.Text = result
                    ElseIf result = appsettings("key2") Then
                        TextBox2.Text = result
                    End If
                Next
    End If
    

    If you can download Visual Studio Community 2017 (it's free) and set Option Strict on, it will be a big help to you coding. Code is not tested against app.config.