locked
ConnectionString in ctor for ctrl derived from SqlDataSource RRS feed

  • Question

  • User304384516 posted

    I have a custom server control derived from SqlDataSource. It is in a dll. I reference the dll in a web site. In the ctor, I want to get a connection string from web.config. I have tried

    ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;

    When I drop an instance of the control on a web form, I get the error "Object reference not set to an instance of an object.". I seems like the control can't read web.config. How can I read a connection string from web.config in a custom server control at design time?

    Bill

    Tuesday, August 30, 2011 4:11 PM

Answers

  • User304384516 posted

    I now access the connection string in the Init handler. It seems to work

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2011 4:04 PM

All replies

  • User1039463668 posted

    When you invoke any methods from the DLL, you should pass the connection string to it as an object:

    MyConnectionString {    
     get     {      
       return ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString   
              } 
    } 

     Please refer here..

    http://msdn.microsoft.com/en-us/library/ms972970.aspx

    Wednesday, August 31, 2011 2:33 AM
  • User3866881 posted

    Hello swartzbill2000:)

    I think you should import the dll control into your test proj, and in your test proj there's a web.config with the configured connectionString tags.

    That's OK.

    Wednesday, August 31, 2011 11:03 PM
  • User304384516 posted

    I now access the connection string in the Init handler. It seems to work

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2011 4:04 PM