0

I am working in Microsoft Visual Web Developer 2010 express on an asp.net website. I got following codes;

In file Web Config

<configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ReplaceToken\abcDATA.accdb;Persist Security Info=True;Jet OLEDB:Database Password=abc"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <system.web>
        <compilation debug="false" strict="false" explicit="true" targetFramework="4.0" />
    </system.web>
</configuration>

In file Default.aspx

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="SqlDataSource1" DataTextField="PickList" 
    DataValueField="PickList"> 
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT [PickList] FROM [T_BAS_PASexport]">
</asp:SqlDataSource>

In Web Config file on ConnectionString line I want to replace the word ReplaceToken to the current directory path on application/website run. Please can someone help me that how can I do this. What code I need to put in my Default.aspx.vb file and after that what I need to change in my Default.aspx file. A brief explanation would be much appreciated.

kami
  • 259
  • 4
  • 13

1 Answers1

0

Try this:

 //In file Default.aspx.cs
 public static string CustomConnectionString
 {
     get
     {
        var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        connectionString.Replace("ReplaceToken", "TODO");
        return connectionString;
     }
 }

 //In file Default.aspx
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%= CustomConnectionString %>" 
Jose M.
  • 1,296
  • 17
  • 22
  • Thanks for replying Jose. Please can you give solution in VB.net as I don't now much about C#. – kami Nov 26 '14 at 22:17
  • I'm sorry but I do not know much about vb. Try this [http://converter.telerik.com/](http://converter.telerik.com/) – Jose M. Nov 26 '14 at 22:20
  • Its not working. Getting the following error. An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'. – kami Nov 27 '14 at 10:24
  • [http://stackoverflow.com/questions/...](http://stackoverflow.com/questions/23192938/an-ole-db-provider-was-not-specified-in-the-connectionstring-provider-sqloledb) – Jose M. Nov 27 '14 at 10:32
  • Nothing is working. Doesn't matter what I try, I get new error message. Some time its Provider sometime its "<%" sign and many other errors. There must be some way to achieve this. I need some kind of VB.Net code which should replace the word ReplaceToken to the current directory path (Server.Mappath ("~")) etc and then some to go in line ConnectionString="<%$ ConnectionStrings:ConnectionString %>" to achive this. Thanks Jose but your code is not helping. – kami Nov 27 '14 at 21:20