How to access connection string from web.config in SharePoint 2007 Webpart?


  • Hi,

    I have a code in Webpart (Sharepoint 2007) xml below

    <WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="0" Title="BranchDS" __markuptype="vsattributemarkup" __WebPartId="{6C054FD5-6B3F-43C8-B2DB-722FDC32FDCA}" id="g_6c054fd5_6b3f_43c8_b2db_722fdc32fdca" __AllowXSLTEditing="true" WebPart="true" Height="" Width="">
    <asp:SqlDataSource ID="BranchDS" runat="server"  ProviderName="System.Data.SqlClient"     ConnectionString="<%$ connectionStrings:MyConnectionString %>"   SelectCommand="SELECT * FROM Branches" >

    I have created a connectionstring in web.config with name MyConnectionString.

    When I tries to execute the above code it throws an error

    Format of the initialization string does not conform to specification starting at index 0.

    Anyone has the solution please reply

    Thanks in Advance...

    Ravi Patil
    Tuesday, December 21, 2010 11:45 AM


  • I got the solution just need make the following one line change

    ConnectionString="&lt;%$ connectionStrings:MyConnectionString %&gt;

    instead of

    ConnectionString="<%$ connectionStrings:MyConnectionString %>

    Ravi Patil
    • Marked as answer by Ravi_Patil Wednesday, March 09, 2011 7:01 AM
    Wednesday, March 09, 2011 7:01 AM

All replies