This
article will show you how you can retrieve the connection string value defined
in appsettings and connectionstrings tag in your web.config file in asp.net
application using c#.net.
Some of my
previous articles are as follows: Bind
GridView To DataTable and Convert GridView Data Into Chart In Asp.net Using
jQuery, Li
Html Tag Styling By Css3 With Dynamic Circular Item Count In Asp.Net In Asp.Net, jQuery
DateTime Picker Calendar With Plush & Minush Date Changer In Asp.Net, Dynamic
Thumbnails Image Preview Slider With Zoom Effect Using jQuery In Asp.Net and
C#.Net, Sign
Up Form For Newsletter In Asp.Net Using C# and Css3, Stylish
Button Using Css3 In Asp.net, How
to Get Mouse Pointer Cordinate Inside a Div Using jQuery In Asp.Net.
So for this
article first we will create a new asp.net application and add the below code
into your web.config file.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<appSettings>
<add key="myconnectionstring" value="Data Source=Server;Integrated
Security=true;Initial Catalog=DBappSettings"/>
</appSettings>
<connectionStrings>
<add name="myconnectionstring" connectionString="Data
Source=Server;Integrated Security=true;Initial Catalog=DBConnectionStrings"/>
</connectionStrings>
</configuration>
|
In above code please check the appsettings and connectionstrings. Now I will tell you how you will retrieve the value from web.config file.
Now add a
new web page and add the below code into the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Retrieve Connection
String From Web.config In ASP.net Using C#.Net
(AppSettings,ConnectionStrings) </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="From AppSettings" OnClick="Button1_Click"
/>
<asp:Button ID="Button2" runat="server" Text="From
ConnectionStrings" OnClick="Button2_Click" />
<br />
<br />
AppSettings Value:
<asp:Label ID="Label1" runat="server" Text="Label" style="font-weight:
700"></asp:Label>
<br />
ConnectionStrings Value:
<asp:Label ID="Label2" runat="server" Text="Label" style="font-weight:
700"></asp:Label>
</div>
</form>
</body>
</html>
|
Now
generate the button click event and add the below code.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication3
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string val =
System.Configuration.ConfigurationManager.AppSettings["myconnectionstring"].ToString();
Label1.Text = val;
}
protected void Button2_Click(object sender, EventArgs e)
{
string val =
System.Configuration.ConfigurationManager.ConnectionStrings["myconnectionstring"].
ToString();
Label2.Text = val;
}
}
}
|
In above code i have retrieve the appsettings and connectionstrings on button click and display into the label control. Now we have
done run the application to check the output.
0 comments:
Please let me know your view