By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

How to use function in ConnectionString property in SQLDataSource Webcontrol ?

P: n/a
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.ConfigurationM anager.AppSettings("OrderingProcess"))
The question is: how could I use this function in
ConnectionString="<%$ ConnectionStrings:OrderingProcess %>"
above ?

I cannot say
ConnectionString="<%=
DecryptSetting(System.Configuration.ConfigurationM anager.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.ConfigurationM anager.AppSettings("ConnectionString"))

Thanks a lot for your time and effort.
Alex.

May 14 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Radu" wrote:
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.ConfigurationM anager.AppSettings("OrderingProcess"))
The question is: how could I use this function in
ConnectionString="<%$ ConnectionStrings:OrderingProcess %>"
above ?

I cannot say
ConnectionString="<%=
DecryptSetting(System.Configuration.ConfigurationM anager.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.ConfigurationM anager.AppSettings("ConnectionString"))

Thanks a lot for your time and effort.
Alex.

May 15 '07 #2

P: n/a
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") %>

"Peter Bromberg [C# MVP]" wrote:
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
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Radu" wrote:
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.ConfigurationM anager.AppSettings("OrderingProcess"))
The question is: how could I use this function in
ConnectionString="<%$ ConnectionStrings:OrderingProcess %>"
above ?

I cannot say
ConnectionString="<%=
DecryptSetting(System.Configuration.ConfigurationM anager.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.ConfigurationM anager.AppSettings("ConnectionString"))

Thanks a lot for your time and effort.
Alex.
May 15 '07 #3

P: n/a
On May 14, 10:28 pm, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
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 storethe
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") %>

"Peter Bromberg [C# MVP]" wrote:
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
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"Radu" wrote:
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.ConfigurationM anager.AppSettings("Order*ingProcess"))
The question is: how could I use this function in
ConnectionString="<%$ ConnectionStrings:OrderingProcess %>"
above ?
I cannot say
ConnectionString="<%=
DecryptSetting(System.Configuration.ConfigurationM anager.AppSettings("Order*ingProcess"))
%>"
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.ConfigurationM anager.AppSettings("Conne*ctionString"))
Thanks a lot for your time and effort.
Alex.- Hide quoted text -

- 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 <expressionBuilderssection 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.

May 15 '07 #4

P: n/a
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).Con nectionString
' 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

"Radu" wrote:
On May 14, 10:28 pm, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
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") %>

"Peter Bromberg [C# MVP]" wrote:
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
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"Radu" wrote:
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.ConfigurationM anager.AppSettings("OrderĀ*ingProcess"))
The question is: how could I use this function in
ConnectionString="<%$ ConnectionStrings:OrderingProcess %>"
above ?
I cannot say
ConnectionString="<%=
DecryptSetting(System.Configuration.ConfigurationM anager.AppSettings("OrderĀ*ingProcess"))
%>"
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.ConfigurationM anager.AppSettings("ConneĀ*ctionString"))
Thanks a lot for your time and effort.
Alex.- Hide quoted text -
- 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 <expressionBuilderssection 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.

May 16 '07 #5

P: n/a
On May 15, 9:41 pm, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
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).Con nectionString
' 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

"Radu" wrote:
On May 14, 10:28 pm, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
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") %>
"Peter Bromberg [C# MVP]" wrote:
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
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"Radu" wrote:
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.ConfigurationM anager.AppSettings("Order**ingProcess"))
The question is: how could I use this function in
ConnectionString="<%$ ConnectionStrings:OrderingProcess %>"
above ?
I cannot say
ConnectionString="<%=
DecryptSetting(System.Configuration.ConfigurationM anager.AppSettings("Order**ingProcess"))
%>"
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.ConfigurationM anager.AppSettings("Conne**ctionString"))
Thanks a lot for your time and effort.
Alex.- Hide quoted text -
- 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 <expressionBuilderssection 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.- Hide quoted text -

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

Jun 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.