Access webconfig appsettings Silverlight Application


In every ASP.Net application we used to access webconfig appsettings key values for use sql connection and other key values which have to be controlled from out of page. So to achieve this in ASP.Net we use the following command


This gives the value which has been written in webconfig like,

<appSettings file="">
add key="ConStr" value="Data Source=DataSourceName;
Initial Catalog=DataBaseName;User ID=UserName;Password=Password

But in silverlight application we cant use appsettings as like application. Because the silver light application is a client side application. So in this article we are going to see how to access appsettings in silverlight application.

Code in WebConfig:

<appSettings file="">
add key="SampleVal" value="1"/>

Here specify which value you are going to use in silver light application.In add tab Key is the name of the key and value is the accessing value of the key.

Pass The config value to aspx page:

To pass the config value add param in silverlight application object.

<param name="initParams" value=" SampleVal =<%= System.Configuration.ConfigurationManager.AppSettings["SampleVal "]%>"/>

To get the value in xaml page:

To use the key value in MainPage.xaml first get it in app.xaml and pass it to MainPage.xaml.
In App.Xml get the value by below code.

public string SampleVal ="";
private void Application_Startup(object sender, StartupEventArgs e)
                 SampleVal = e.InitParams["SampleVal "];
                this.RootVisual = new MainPage();

To use value in MainPage.xaml

App application = (App)Application.Current;
String finalval= application. SampleVal;

Up Next
    Ebook Download
    View all
    View all