How to use function in ConnectionString property in SQLDataSource Webcontrol ?

R

Radu

Hi. In a repeater I have as ItemTemplate the following, among others,
and everything works great:

<asp:SqlDataSource
ID="LocationSqlDataSource"
SelectCommand="SELECT blah-blah-blah"
EnableCaching="True"
ConnectionString="<%$ ConnectionStrings:OrderingProcess %>"
CacheDuration="60"
FilterExpression="ID = {0}" RunAt="server">

<FilterParameters>
<asp:ControlParameter ControlID="HyperLinkMeasureID"
PropertyName="Text" /> </FilterParameters>
</asp:SqlDataSource>
etc.


I have encrypted in Base64 the "OrderingProcess" which was
<add name="OrderingProcess" connectionString="server=xxx
\xxx;database=xxx;User ID=xxx;Password=xxx;"
providerName="System.Data.SqlClient" />

and the "ConnectionString" which was
<add key="ConnectionString" value="Data Source=xxx\xxx;Initial
Catalog=xxx;User ID=xxx;Password=xxx;"/>

and in the code-behind I have a function named DecryptSetting which
can be used as:

DecryptSetting(System.Configuration.ConfigurationManager.AppSettings("OrderingProcess"))


The question is: how could I use this function in
ConnectionString="<%$ ConnectionStrings:OrderingProcess %>"
above ?

I cannot say
ConnectionString="<%=
DecryptSetting(System.Configuration.ConfigurationManager.AppSettings("OrderingProcess"))
%>"


In all the places in code behind where I needed the connection string,
I have used
Cache("ConnectionString") which has been set in App_Init as:

HttpContext.Current.Cache("ConnectionString") =
DecryptSetting(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))

Thanks a lot for your time and effort.
Alex.
 
G

Guest

How about something like:

ConnectionString="<%$ DecryptSettings(ConnectionStrings:OrderingProcess) %>"
-- the syntax may not be exactly right, but this approach works fine with
many <%--%> delimited items.
Peter
 
G

Guest

You could also create a public static (Shared in VB) function (in App_Code)
which will return decrypted connection string based on a string name
parameter, but will only read the config once per request and store them in a
Generic string dictionary for subsequent calls. Then you can either store the
dictionary in a static variable, or even application Cache.

Then from your aspx pages you could call this function similar to the
following:
ConnectionString="<%= GetMyConnect("OrderingProcess") %>
 
R

Radu

You could also create a public static (Shared in VB) function (in App_Code)
which will return decrypted connection string based on a string name
parameter, but will only read the config once per request and store them in a
Generic string dictionary for subsequent calls. Then you can either store the
dictionary in a static variable, or even application Cache.

Then from your aspx pages you could call this function similar to the
following:
ConnectionString="<%= GetMyConnect("OrderingProcess") %>

:






- Show quoted text -


Thank you for answering !

I have tried with
ConnectionString="<%=
DecryptSetting(ConnectionStrings:OrderingProcess) %>"

and it compiles, but when time comes to serve that page, I get
Keyword not supported: '<%'.


When I try with
ConnectionString="<%$
DecryptSetting(ConnectionStrings:CNOrderingProcess) %>"
(the only change is the '$' sign instead of '='), then I get 98 errors
and warnings, all having NOTHING to do with the problem at hand
(example: Message 88 Could not find schema information for the
element 'http://schemas.microsoft.com/.NetConfiguration/
v2.0:customErrors'.) - thank you, compiler, but I also get the real
problem:
Error 90 The expression prefix 'DecryptSetting(ConnectionStrings'
was not recognized. Please correct the prefix or register the prefix
in the <expressionBuilders> section of configuration.


The function DecryptSetting is ultra-simple, in MAIN.vb in APP_CODE:

Public Function DecryptSetting(ByVal Source As String) As String

Dim data() As Byte = Convert.FromBase64String(Source)
Return System.Text.ASCIIEncoding.ASCII.GetString(data)
End Function


Thanks again
Alex.
 
G

Guest

Alex,

Quote from MSDN:
"The dollar sign ($) indicates to ASP.NET that an expression follows. The
expression prefix defines the type of expression, such as AppSettings,
ConnectionStrings, or Resources. Following the colon :)) is the actual
expression value that ASP.NET will resolve."

That is the reason why the compiler complains when you are trying <%$
Deccrypt ... your connection - it is not one of the predefined

If you want to call a function from HTML then you would use <%= syntax, but
then the parser will not recognize ConnectionStrings:MyStringName syntax

You could do something in between:
- declare your connection string in web.config as <add name="myName"
connectionString="myString" providerName="myProvider"
- call your shared function from html with <%= syntax passing the connection
name as the parameter: <%= MyDecrypt("myName") %>
- in decrypt function use ConfigurationManager to retrieve the connection
string
Public Shared Function MyDecrypt(cnName As String) As String
Dim result As String =
ConfigurationManager.ConnectionStrings(cnName).ConnectionString
' perform the decryption: result = decrypted(result)
Return result
End Function

There may be a better way to do it - but this should get you over the line.
Personally, I do not use DataSources - hence I am not the best source of
info on the topic.

Hope this helps
 
R

Radu

Alex,

Quote from MSDN:
"The dollar sign ($) indicates to ASP.NET that an expression follows. The
expression prefix defines the type of expression, such as AppSettings,
ConnectionStrings, or Resources. Following the colon :)) is the actual
expression value that ASP.NET will resolve."

That is the reason why the compiler complains when you are trying <%$
Deccrypt ... your connection - it is not one of the predefined

If you want to call a function from HTML then you would use <%= syntax, but
then the parser will not recognize ConnectionStrings:MyStringName syntax

You could do something in between:
- declare your connection string in web.config as <add name="myName"
connectionString="myString" providerName="myProvider"
- call your shared function from html with <%= syntax passing the connection
name as the parameter: <%= MyDecrypt("myName") %>
- in decrypt function use ConfigurationManager to retrieve the connection
string
Public Shared Function MyDecrypt(cnName As String) As String
Dim result As String =
ConfigurationManager.ConnectionStrings(cnName).ConnectionString
' perform the decryption: result = decrypted(result)
Return result
End Function

There may be a better way to do it - but this should get you over the line.
Personally, I do not use DataSources - hence I am not the best source of
info on the topic.

Hope this helps













- Show quoted text -

Thanks, you're right, this should work - it's logical. Thank you very
much.
Alex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top