Saturday, 23 April 2016

Retrieve Connection String From Web.config In ASP.net Using C#.Net (AppSettings,ConnectionStrings)

4/23/2016 - By Pranav Singh 0

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.

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" />
            &nbsp;<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.









Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top