How to get the connection string  depending upon the url parameter passed in c#,

Posted on 2014-08-04
Last Modified: 2016-06-10

I am having ItemList Application. This application is used by 20 applications and each of them have separate database. From each 20 applications, I want to open the ItemList application and value is inserted in the corresponding database.

ItemList application is not having its own database. ItemList is using the database depending upon the application called.

 <A href="ItemList/Login.aspx?AppID=CallApp" target="_self">ItemList</A>

How this can be achieved?
Question by:Saroj13
    LVL 16

    Accepted Solution

    All the 20 applications connection strings could be either saved in config file or in database. Say for example in config file use different keys in <appsettings> section.

    <appsetting key="App1"  value="ConnectionString1">

    Likewise if you want in database

    ID Value
    App1 ConnnectionString1

    Then read the value from the config value or from DB and use accordingly.

    You have some other options as well. Store the connection strings in XML file and read the XML file and store it in dictionary object or Cache. So that you could read from there at ease.  This is applicable if the connection strings are not subject to frequent change.

    LVL 52

    Assisted Solution

    by:Carl Tawn
    Is the whole connection string different, or is it just the database name?

    If it's just the database name, you could store a parameterised connection in the config file and simply substitute the database name at runtime:
    // example config file
        <add name="AppConnection" connectionString="Server=SomeServer;Initial Catalog={0};User ID=blah;PWD=blah;" providerName="System.Data.SqlClient" />

    Open in new window

    Then in code:
    string appName = "CallApp";
    string connectionString = string.Format(ConfigurationManager.ConnectionStrings["AppConnection"], appName);

    Open in new window

    Of course you wouldn't really want to be passing database names around in visible code, so a table of pseudonyms for application/database name pairs would be appropriate.

    Author Comment

    ItemList application is ued by 20 applications. Each 20 application is having corresponding Database.

    <A href="ItemList/Login.aspx?AppID=App1" target="_self">ItemList</A>
    App1 is having Database App1. App1 Database is having 3 corresponding ItemList tables.

    <A href="ItemList/Login.aspx?AppID=App2" target="_self">ItemList</A>
    App2 is having Database App2. App2 Database is having 3 corresponding ItemList  tables.

    ItemList application, depending upon the Parameter passed in the url, use the corresponding database. If Itemlist app is open from App1 application, then Itemlist app uses App1 Database.

    If Itemlist app is open from App2 application, then Itemlist app uses App2 Database.

    ItemList is the generic application.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now