Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6726001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:52:28+00:00 2026-05-26T09:52:28+00:00

If I have a connection string defined in my web.config file, how do I

  • 0

If I have a connection string defined in my web.config file, how do I create a connection to the SQL db from C# code (sorry forgot to specify) and then call a stored procedure. I would then like to eventually use this data in some way as my DataSource for a GridView.

Here is how the connection string is defined in the web.config:

<connectionStrings>
 <add name="db.Name" connectionString="Data Source=db;Initial Catalog=dbCat;User ID=userId;Password=userPass;" providerName="System.Data.SqlClient" />
 </connectionStrings>

The db server is a Microsoft SQL server.

Here is what I was looking for:

ConnectionStringSettings conSet = ConfigurationManager.ConnectionStrings["db.Name"];
SqlConnection con = new SqlConnection(conSet.ConnectionString);

The code to get the data is fairly trivial. I was more interested in accessing it from a connectionString variable in the web.config file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:52:29+00:00Added an answer on May 26, 2026 at 9:52 am

    If it’s a resource file like so:

    private static readonly string connString = Resource1.connString;

    Where connString is the name of the key. If it is a web.config file

    Something like so:

    private static readonly string connString = System.Configuration.ConfigurationManager.AppSettings["strConn"]; where conn is defined in your web config file.

    <add key="strConn" value="User ID=test;Password=test;Initial Catalog=TestDB;Data Source=NameOfServer;"/>
    

    Then call the sproc:

      //connString = the string of our database app found in the resource file
                    using (SqlConnection con = new SqlConnection(connString))
                    {
                        using (SqlCommand cmd = new SqlCommand("EMPDLL_selClientByClientID", con))
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Add("@ClientID", SqlDbType.VarChar).Value = cID;
                            con.Open();
    
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                if (reader.HasRows)
                                {
                                    if (reader.Read())
                                    {
                                           //more code
                                    }
                                 }
                            }
                         }
                      }
    

    That’s if you are coding in C#, VB.net its the same deal just a bit more wordier :), here’s a small sample:

     Public Sub DeleteEmployee(ByVal lVID As Long)
            Dim conMyData As SqlConnection
            Dim cmdDelete As SqlCommand
    
            Try
                conMyData = New SqlConnection(connString)
                cmdDelete = New SqlCommand("delEmployee", conMyData)
    
                With cmdDelete
                    .CommandType = CommandType.StoredProcedure
                    'add the parameters
                    .Parameters.Add("@LoginID", SqlDbType.BigInt).Value = lVID    'the request
                    conMyData.Open()    'open a connection
                    .ExecuteNonQuery()  'execute it
                End With
    
            Catch ex As Exception
                Throw ex
            Finally
                cmdDelete = Nothing
                conMyData.Close()
                conMyData = Nothing
            End Try
        End Sub
    

    Of course you should use a using statement instead of try/catch/finally to ensure you clean up your resources that are being used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my applications web.config file I have a connection string stored. I encrypted it
I have a connection string defined in my web.config like: <connectionStrings> <add name=LibraryConnectionString connectionString=Server=.\SQLEXPRESS3;Database=Library;Integrated
I have the following ASP.NET Membership section defined in the Web.config file: <membership defaultProvider=AspNetActiveDirectoryMembershipProvider>
I'm currently attempting to read information about an object from the web.config file and
I have my NHibernate configuration successfully set up in my web.config file. However, I
I have just joined a project that has a connection string which is defined
I have a connection string being passed to a function, and I need to
I have the following connection string: Data Source=localhost\\SQLEXPRESS; , it complains that '' is
I have the following connection string, and you will notice Provider's.Tests, notice the single
I have been tasked with securing the connection string in an classic ASP application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.