none
Why do asp:SQLDataSource and C# connectionString point to two different configs? RRS feed

  • Question

  • Hi there,

    I have a VS2010 website project that includes the ASPNETDB.MDF database. In the project package/publish settings I selected the option Exclude files from App_Data folder and when importing the package into IIS7.5 the database is successfully created in the Users\AppData\... folder with a different name (XYZWebDB).

    The site opens correctly and pages that have an asp:SQLDataSource element retrieve data correctly based on the connection string name (from web.config) and SQL select command.

    Other pages that retrieve the connection string with C# simply hang and don’t do anything. The connection string was copied from MSDB Library and is shown below.

    The above left me with the following questions:

    1. It appears the  C# connection string looks at a different config(?) file information compared to the asp:SQLDataSource element. It is still looking for the ASPNETDB database, why?
    2. Where else is this information stored?

    Any help would be very much appreciated and many thanks in advance to all the people reading this.

    Have a wonderful day…

    Regards

    PS The two connections are shown below...

    The asp:SQLDataSource uses the named instance from the web.config and is configured as follows:

    <asp:SqlDataSource ID="SqlDataSourcePCBProduct" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:ApplicationServices%>" 
    SelectCommand="SELECT Product, AUDinclGST FROM tbl_PCBproduct WHERE ProductID < 11"
    runat="server"></asp:SqlDataSource>
    

    The C# code was copied from the MSDN Library and is as follows:

        // Get SQL Connection String
        static private string dsGetConnectionString()
        {
          // Get the application path needed to obtain
          // the application configuration file.
          string appName = Environment.GetCommandLineArgs()[0];
          string appexePath = System.IO.Path.Combine(Environment.CurrentDirectory, appName);
          // Get the application configuration file.
          System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(appexePath);
          // Get the connectionStrings section.
          ConnectionStringsSection csSection = config.ConnectionStrings;
          // Select the section.
          ConnectionStringSettings cs = csSection.ConnectionStrings[0];
          //  Return connection string.
          return cs.ConnectionString;
        }
    

    Michael

    • Moved by Mike Dos Zhang Tuesday, July 31, 2012 8:54 AM (From:Visual C# Express Edition)
    Sunday, July 29, 2012 7:51 AM

Answers

  • After a long search I found the solution and want to share it. The C# code above was looking at the application.exe and the below is now looking at the connection string in the web.config file and it works correctly in IIS7.5.

    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString.ToString());
    

    • Marked as answer by Michael754 Thursday, August 2, 2012 10:35 AM
    Thursday, August 2, 2012 10:35 AM

All replies

  • Hi Michael ,

     You may have more luck getting answers in the ASP forums rather than the C# forums.

     Have a good day!

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 30, 2012 4:56 AM
  • After a long search I found the solution and want to share it. The C# code above was looking at the application.exe and the below is now looking at the connection string in the web.config file and it works correctly in IIS7.5.

    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString.ToString());
    

    • Marked as answer by Michael754 Thursday, August 2, 2012 10:35 AM
    Thursday, August 2, 2012 10:35 AM