SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for ASP.Net, SQL Server, Reporting Services, T-SQL, Windows, AWS, SAP HANA and ABAP


How to Read a String Array from Web.Config file appSettings Section


You may want to keep a list of values probably which can be stored in an array of string in your application as a lookup table or lookup variable.
Developers may use this string array to store list of valid values for a variable.
I had developed an application which I can upload files to a SQL Server database. And I want to check the file extension, then compare its extension with the valid or allowed file extensions list. I want to keep this value parametric so I can make changes to this string array list easily. So I preperred the web.config as the string array storage.

In the <appSettings> section, I had declared the AllowedExtensionsList key as follows:

<add key="AllowedExtensionsList" value=".jpg,.jpeg,.png,.gif" />
Code

You should trim the spaces between comma seperators in the list otherwise spaces will be part of the splitted array string items.

Dim allowedExtensions As String()
With New AppSettingsReader
    allowedExtensions = Split(.GetValue("AllowedExtensionsList", GetType(String)), ",")
End With
Code




Note that you might want to trim the values that are read from the web.config file in case values are not supplied in the correct format.
Also you might want to read the string array from the web.config file once and store it as an Application variable. And read string list from the Application object after you read it for the first time from the web.config configuration file.

Dim allowedExtensions As String()
If Application("allowedExtensions") Is Nothing Then
   With New AppSettingsReader
      Application("allowedExtensions") = Split(.GetValue("AllowedExtensionsList", GetType(String)), ",")
      Dim i As Int32 = 0
      While i < DirectCast(Application("allowedExtensions"), String()).Length
         Application("allowedExtensions")(i) = Application("allowedExtensions")(i).ToString.Trim
         i = i + 1
      End While
   End With
End If
allowedExtensions = Application("allowedExtensions")
Code


SQL Server

SQL Server 2019 Installation
download SQL Server 2019
download SQL Server 2017
download SQL Server 2016
download SQL Server 2014
download SQL Server 2012
MacOS ve SQL Server 2019


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.