Access webconfig appsettings Silverlight Application


Introduction:

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

"System.Configuration.ConfigurationManager.AppSettings["ConStr"]"

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

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

But in silverlight application we cant use appsettings as like asp.net 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="">
    <clear/>
    <
add key="SampleVal" value="1"/>
 </appSettings>

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;
 


Similar Articles