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 966151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:06:40+00:00 2026-05-16T02:06:40+00:00

How can I make the code string connStr = ConfigurationManager.ConnectionStrings staceys_cakesConnectionString].ConnectionString; work generically and

  • 0

How can I make the code

string connStr = ConfigurationManager.ConnectionStrings "staceys_cakesConnectionString"].ConnectionString; 

work generically and not need the staceys_cakesConnectionString? Or how can I set it somewhere else so I only have to change it one place when I change it?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace SC1.Models.DAL 
{ 
  public class CategoryDAL 
  { 
    public CategoryDAL() 
    { 
    } 
    string connStr = ConfigurationManager.ConnectionStrings["staceys_cakesConnectionString"].ConnectionString; 

    // select all 
    public DataSet Select() 
    { 
      SqlConnection sqlConnection1 = new SqlConnection(); 
      string SqlString = "select * from Categories"; 
      SqlDataAdapter da = new SqlDataAdapter(SqlString, connStr); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "Categories"); 
      return (ds); 
    } 
    // save 
    // insert 
    // update 
    // delete 

  } 
} 

Example of a page function I have how can I make this one better using your suggestion @Adam or anyone else?

   // List
    public List<page> Select()
    {
      List<page> _list = new List<page>();
      string  SqlStatement = "select * from Pages";
      SqlConnection thisConnection = new SqlConnection(connStr);
      // Open the Connection
      thisConnection.Open();

      var thisCommand = thisConnection.CreateCommand();
      thisCommand.CommandText = SqlStatement;
      SqlDataReader thisReader = thisCommand.ExecuteReader();

      while (thisReader.Read())
      {
        // Create a new instance of the Current Page Object
        page currentPage = new page();
        // Fill the instance of the Current Page Object
        currentPage.PageID = Convert.ToInt32(thisReader["PageID"]);
        currentPage.ParentID = Convert.ToInt32(thisReader["ParentID"]);
        currentPage.CategoryID = Convert.ToInt32(thisReader["CategoryID"]);
        currentPage.Name = thisReader["Name"].ToString();
        currentPage.PageHTMLContent = thisReader["PageHTMLContent"].ToString();
        currentPage.NavigationText = thisReader["NavigationText"].ToString();
        currentPage.TopMenu = Convert.ToBoolean(thisReader["TopMenu"]);
        currentPage.SubMenu = Convert.ToBoolean(thisReader["SubMenu"]);
        currentPage.DisplayOrder = Convert.ToInt32(thisReader["DisplayOrder"]);
        currentPage.Active = Convert.ToBoolean(thisReader["Active"]);
        // Add the instance of the Current Page Object to the List<>.
        _list.Add(currentPage);
      }
      // Close the Database
      thisConnection.Close();
      return _list;      

    }
  • 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-16T02:06:40+00:00Added an answer on May 16, 2026 at 2:06 am

    Just use a constant. For that matter, just use a static property and obtain the string that way.

    public static class ConnectionStrings
    {
        public static string StacyesCakes 
        { 
            get 
            { 
                ConfigurationManager.ConnectionStrings[
                      "staceys_cakesConnectionString"].ConnectionString; 
            }
        }
    }
    

    That will allow you to do things like:

    using(var conn = new SqlConnection(ConnectionStrings.StaceysCakes))
    {
        ...
    }
    

    Or (just adapting your existing code):

    public DataSet Select() 
    { 
      SqlConnection sqlConnection1 = new SqlConnection(); 
      string SqlString = "select * from Categories"; 
      SqlDataAdapter da=new SqlDataAdapter(SqlString,ConnectionStrings.StaceysCakes); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "Categories"); 
      return (ds); 
    } 
    

    (You don’t need sqlConnection1; you’re not using it anywhere).

    Note, however, that because SqlDataAdapter implements IDisposable and you’re finished with it after this code executes, you should enclose it in a using block.

    I would rewrite your function to something like this:

    public DataSet Select() 
    { 
      using(SqlDataAdapter da = new SqlDataAdapter(
                                "select * from Categories",
                                ConnectionStrings.StaceysCakes))
      {
          DataSet ds = new DataSet(); 
          da.Fill(ds, "Categories"); 
          return ds;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I make this code work? #!/bin/bash ARRAYNAME='FRUITS' FRUITS=( APPLE BANANA ORANGE )
how can i make my code to work ? :) i`ve tried to formulate
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
How can I make the following code work? During compilation I get an error
I can make this code work without an object as input parameter for the
How can I make the code below into a function? # split the string
I have next code @XmlAttribute private String workstation; Can I make constraints for this
How can I make my strings and text attributes bold in my actionscript code?
How can I make sure in the following code snippet that IDataReader is disposed
How can I make sure the CS generated from code like the following is

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.