Read contents from AppSetting using Split function in ASP.Net using C# and VB.Net

Last Reply 10 months ago By kalpesh

Posted 10 months ago

hello there,

 

how to read to contents from this app.settings and display it as label.text on web site.

thanks.

string[] mykey = ConfigurationManager.AppSettings["mykey"].Split(',');
You are viewing reply posted by: kalpesh 10 months ago.
Posted 10 months ago Modified on 10 months ago

You can access by its index value like below.

HTML

<asp:Label ID="lblValue1" runat="server"></asp:Label>
<asp:Label ID="lblValue2" runat="server"></asp:Label>
<asp:Label ID="lblValue3" runat="server"></asp:Label>
<asp:Label ID="lblValue4" runat="server"></asp:Label>
<asp:Label ID="lblValue5" runat="server"></asp:Label>

C#

string[] mykey = ConfigurationManager.AppSettings["mykey"].Split(',');
    
// By for loop
for (int i = 0; i < mykey.Length; i++)
{
    switch (i)
    {
        case 0:
            lblValue1.Text = mykey[i].ToString();
            break;
        case 1:
            lblValue2.Text = mykey[i].ToString();
            break;
        case 2:
            lblValue3.Text = mykey[i].ToString();
            break;
        case 3:
            lblValue4.Text = mykey[i].ToString();
            break;
        case 4:
            lblValue5.Text = mykey[i].ToString();
            break;
    }
}

// By setting index
lblValue1.Text = mykey[0].ToString();
lblValue2.Text = mykey[1].ToString();
lblValue2.Text = mykey[2].ToString();
lblValue4.Text = mykey[3].ToString();
lblValue5.Text = mykey[4].ToString();

 VB.Net

Dim mykey As String() = ConfigurationManager.AppSettings("mykey").Split(","c)
For i As Integer = 0 To mykey.Length - 1
    Select Case i
        Case 0
            lblValue1.Text = mykey(i).ToString()
        Case 1
            lblValue2.Text = mykey(i).ToString()
        Case 2
            lblValue3.Text = mykey(i).ToString()
        Case 3
            lblValue4.Text = mykey(i).ToString()
        Case 4
            lblValue5.Text = mykey(i).ToString()
    End Select
Next

lblValue1.Text = mykey(0).ToString()
lblValue2.Text = mykey(1).ToString()
lblValue2.Text = mykey(2).ToString()
lblValue4.Text = mykey(3).ToString()
lblValue5.Text = mykey(4).ToString()