AspnetO

We code, that works!

  • Home
  • Asp.net
  • MVC
  • Interview Questions
You are here: Home / Asp.net / How to Get ConnectionString from Web.config in Asp.net?

How to Get ConnectionString from Web.config in Asp.net?

By: Mayank Modi | Falls In: Asp.net, Interview Questions | Last Updated: Oct 07, 2020

Now here in this tutorial, I’ll explain how to read or get connectionstring from web.config file using c# as well as vb.net in Asp.net. Read the full tutorial to get a clear cut idea on how to obtain connectionstring from web.config file. If you feel that that the tutorial was worth your time, then please do check out our previous tutorials as well as I mentioned below.

In my previous tutorials, I’d explained about appSettings vs connectionStrings in web.config file, sql injection with example and other more amazing tutorials on connectionStrings, Asp.net here.

 

Get ConnectionString from Web.config File

Before starting how to read or get connectionstring from web.config file, you should know how to get connection string to connect your database from here that will show you also the difference why I’ve not used user id and password in my connection string. After knowing that, you can adjust your connection string in web.config as follows.

<connectionStrings>
   <add name=“myDbConnection” providerName=“System.Data.SqlClient”
   connectionString=“Data Source=myServer;Integrated Security=true;Initial Catalog=myDatabase“/>
</connectionStrings>

To read or get connectionstring from web.config file in asp.net, we need to add System.Configuration namespace that will help us to read our connection string.

Note: If you have defined your connection string under appSettings section, you can get your connectionstring from appSettings section this way.

Read Connectionstring in C#

protected void Page_Load(object sender, EventArgs e)
{
string con = System.Configuration.ConfigurationManager.ConnectionStrings[“myDbConnection”].ConnectionString;
}

Read Connectionstring in Vb.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con As String = System.Configuration.ConfigurationManager.ConnectionStrings(“myDbConnection”).ConnectionString
End Sub

Example: Simple Insert, Update and Delete in Asp.net

You may also like insert, update and delete on asp.net gridview here.

That’s it, now you’ll be able get connectionstring from web.config file and can use wherever you want.

Signup Today And Get Latest Tutorials For Free!

Subscribe to us and get free latest tutorials notifications whenever we publish a new contents.

<

About Mayank Modi

Mayank is a web developer and designer who specializes in back-end as well as front-end development. He's a Founder & Chief Editor of AspnetO. If you'd like to connect with him, follow him on Twitter as @immayankmodi.

Leave a Reply Cancel reply

Search Your Topic



Social Connections

  • 1,438 Fans
  • 3,087 Followers
  • 50 Followers
  • 1,559 Subscribers

Get Latest Tutorials For Free



Top Posts

  • Pass Multiple Parameters in Asp.net QueryString Example
  • CSS3 Transition: fadeIn and fadeOut like Effects to Hide Show Elements
  • Show Confirm Message Box from Code-behind in Asp.net
  • Call JavaScript Function from Code-behind in Asp.net C# Vb
  • Send HTML Webpage Content as Email Body in Asp.net C# Vb.net

Contribute to AspnetO

If you want to contribute your unique blog articles or tutorials (Free / Paid) to AspnetO in any languages, you're most welcome. Just send me your previous articles, and topics on which you are interested to post an tutorial. Contact us at email listed in contact us page. Selected candidates will be contacted.

Search by Tags

Ado.net Ajax appSettings Asp.net C# CheckBox CheckBoxList ConnectionStrings Control CSS CSS3 Difference Download DropDownList Export Facebook fadeIn fadeOut fadeTo fadeToggle File File Extension FileUpload Function GridView IIS Interview Questions JavaScript jQuery MVC OOP RadioButtonList RDP Repeater Send Mail Solutions Split SQL Stored Procedure TextBox Upload Validation VB Web.config Web Hosting

The Man Behind AspnetO

Mayank Modi

Hi there,

Myself Mayank Modi, a Full Stack Developer (.NET Stack) and a blogger from Surat, India.

I'm welcoming you to my blog - AspnetO, a programmers community blog where we code, that works!

I started AspnetO as a hobby and now we're growing day by day. We're now having 5000+ programmers that get benefits and learn new things about website design and development under our community blog.

Here at AspnetO, I write about Beginners to Advance level of tutorials on programming languages like Asp.net using C# and Vb.net, MVC, SQL Server, JavaScript, jQuery etc. In sort, all about .NET Framework and website development stuff and sometimes sharing tips and tricks that can help you to grow up your programming skills.

You can get more details about me and my blog at About us page.

Subscribe To Newsletter

Enter your email address to subscribe to this blog and receive notifications of new posts right to your inbox

Join 1000+ other subscribers

<

Recent Posts

  • Main Difference between SessionState and ViewState in Asp.net
  • How to Get appSettings Value from Web.config File?
  • How to Get ConnectionString from Web.config in Asp.net?
  • Difference between appSettings and connectionStrings in Web.config
  • Get Folder Files List and Export to CSV in .NET
  • Get Files List From Directory Recursively in C# Vb.net
  • Get Hash Value From Current Page URL In jQuery
  • Handle Multiple Submit Buttons in Single MVC Form

Copyright © 2014 - 2021 · All Rights Reserved.

About | Copyrights | Privacy | Terms | Contact | Advertise | Sitemap
Previous Difference between appSettings and connectionStrings in Web.config
Next How to Get appSettings Value from Web.config File?