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

The Archive Base Latest Questions

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

I have developed a form that takes from user the user id and password

  • 0

I have developed a form that takes from user the user id and password and display the list of databases available in the local server.Now I have done it in hard coded format…like this

public void BindDBDropDown()
{
    //Create the connection object
    SqlConnection sConnection = new SqlConnection(
        ConfigurationSettings.AppSettings["ConnectionString"]);

    //To Open the connection.
    sConnection.Open();

    //Query to select the list of databases.
    string selectDatabaseNames =
        @"SELECT NAME FROM MASTER..SYSDATABASES";

    //Create the command object
    SqlCommand sCommand = 
        new SqlCommand(selectDatabaseNames, sConnection);

    try
    {
        //Create the data set 
        DataSet sDataset = new DataSet("master..sysdatabases");

        //Create the dataadapter object
        SqlDataAdapter sDataAdapter = 
            new SqlDataAdapter(selectDatabaseNames, sConnection);
        sDataAdapter.TableMappings.Add("Table", 
            "master..sysdatabases");

        //Fill the dataset
        sDataAdapter.Fill(sDataset);

        //Bind the database names in combobox
        DataViewManager dsv = sDataset.DefaultViewManager;

        //Provides the master mapping between the sourcr table 
        //and system.data.datatable
        cmbDatabases.DataSource = 
            sDataset.Tables["master..sysdatabases"];
        cmbDatabases.DisplayMember = "NAME";
        cmbDatabases.ValueMember = ("NAME");
    }
    catch(Exception ex)
    {
        //All the exceptions are handled and written in the EventLog.
        EventLog logException = new EventLog("Application");
        logException.Source = "MFDBAnalyser";
        logException.WriteEntry(ex.Message);
    }
    finally
    {
        //If connection is not closed then close the connection
        if(sConnection.State != ConnectionState.Closed)
        {
            sConnection.Close();
        }
    }
}

/// <summary>
///This function binds the names of all the tables with primary 
///keys in a dropdown cmbResults.
/// </summary>
public void GetPrimaryKeyTable()
{
    //An instance of the connection string is created to manage
    //the contents of the connection string.
    var sqlConnection = new SqlConnectionStringBuilder();
    sqlConnection.DataSource = "192.168.10.3";
    sqlConnection.UserID = "gp";
    sqlConnection.Password = "gp";
    sqlConnection.InitialCatalog = 
        Convert.ToString(cmbDatabases.SelectedValue);
    string connectionString = sqlConnection.ConnectionString;

    SqlConnection sConnection = new SqlConnection(connectionString);

    //To Open the connection.
    sConnection.Open();

    //Query to select the table_names that have PRIMARY_KEYS.
    string selectPrimaryKeys = @"
        SELECT  TABLE_NAME 
        FROM    INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
        WHERE   CONSTRAINT_TYPE = 'PRIMARY KEY'
        AND     TABLE_NAME <> 'dtProperties'
        ORDER BY TABLE_NAME";

    //Create the command object
    SqlCommand sCommand = 
        new SqlCommand(selectPrimaryKeys, sConnection);

    try
    {
        //Create the dataset
        DataSet dsListOfPrimaryKeys = 
            new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");

        //Create the dataadapter object
        SqlDataAdapter sDataAdapter = 
            new SqlDataAdapter(selectPrimaryKeys, sConnection);

        //Provides the master mapping between the sourcr table 
        //and system.data.datatable
        sDataAdapter.TableMappings.Add("Table", 
            "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");

        //Fill the dataset
        sDataAdapter.Fill(dsListOfPrimaryKeys);

        //Bind the result combobox with primary key tables
        DataViewManager dvmListOfPrimaryKeys = 
            dsListOfPrimaryKeys.DefaultViewManager;
        dgResultView.DataSource = dsListOfPrimaryKeys
            .Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
    }
    catch(Exception ex)
    {
        //All the exceptions are handled and written in the EventLog.
        EventLog log = new EventLog("Application");
        log.Source = "MFDBAnalyser";
        log.WriteEntry(ex.Message);
    }
    finally
    {
        //If connection is not closed then close the connection
        if(sConnection.State != ConnectionState.Closed)
        {
            sConnection.Dispose();
        }
    }
}

Can anyone help me in removing these hardcoded things and taking the local server address, userid and password from the app.config file directly???

  • 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-18T09:28:18+00:00Added an answer on May 18, 2026 at 9:28 am

    Sure.

    first, open up your app.config or web.config file.

    Look for the following section

    <appSettings>
        <add key="...." value="....." />
        ....
    </appSettings>
    

    if that doesn’t exist, then it should be added.

    Now add the following key/values…

    <add key="myServer" value="192.168.10.3" />
    <add key="myUserId" value="gp" />
    <add key="myPassword" value="gp" />
    

    and this is an example of what the app.config can now look like…

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <appSettings>  
          <add key="myServer" value="192.168.10.3" />
          <add key="myUserId" value="gp" />
          <add key="myPassword" value="gp" />
       </appSettings>
    </configuration> 
    

    Kewl. Now lets update the code. The trick is to use the ConfigurationManager.AppSettings class :-

    change…

    sqlConnection.DataSource = "192.168.10.3";
    sqlConnection.UserID = "gp";
    sqlConnection.Password = "gp";
    

    to..

    sqlConnection.DataSource = ConfiguationManager.AppSettings["myServer"];
    sqlConnection.UserID = ConfiguationManager.AppSettings["myUserId"];
    sqlConnection.Password = ConfiguationManager.AppSettings["myPassword"];
    

    Now you can change the values of those key/values (in the app.config or web.config) without having to compile the code 🙂

    HTH

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

Sidebar

Related Questions

I have to develop a file server service that transfer file form a specified
I have an existing servlet that has a form and takes in data and
i have developed a simple php contact form, that's working fine, but i am
in my project i have one registration form which is developed in C#.net.to this
I had developed one website purely in HTML .I have my form with fields
I have developed a VB.NET WCF service that recives and sends back data. When
I have a web form with about 15 checkboxes that users may check 0
C# VS 2005. I have developed an application that run perfectly on my development
I have developed a C# form based application. It consists of a number of
I have a script (employment application form) that sends an html email with file

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.