variable Connectionstring= in a <asp:SqlDataSource ... ?

  • Thread starter Thread starter tbh
  • Start date Start date
T

tbh

is it possible in the ConnectionString of an asp:SqlDataSource construction
to refer to a variable i define at the top of my ASPX script and initialize,
for example, during Page_Init()?

(the only examples i have seen use hard-coded references to connection
strings defined in machine.config, e.g.

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings: connStrInMachConfig %>"
not knowing quite how the
<%$ ... %>
magic is definied i'm at a loss to figure it out on my own.
)

i'd be grateful for pointers.

Tim Hanson
 
you cannot use <%= %> with server side controls, and the <%# %> is for
binding. in OnPreInit set the value rom the code behind.

-- bruce (sqlwork.com)
 
thanks, Bruce.

sounds like you know some magic i need. do you have a reference (book, URL)
that explains the rules for (what i take to be 3 forms of magic)
<%$ ... %>
<%= %>
<%# %>
?

cheers,

Tim
 
Hi Tim,

Why not just assign the connection string in code?

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
sqldatasource1.ConnectionString = _
System.Web.Configuration.WebConfigurationManager. _
ConnectionStrings("authorsConnectionString"). _
ConnectionString
End Sub

Ken
Microsoft MVP [ASP.NET]
 
thanks, Ken, that's excactly what i needed -- translated to C# and I used
Page_Init().

in retrospect it seems like a dumb question, but i sometimes have trouble
remembering how the declarative bits relate to the procedural bits, even
though i shouldn't.

cheers,

Tim
 
In Asp.net the WEB.CONFIG file has been created for the purpose of accessing
GLOBAL Application variables. This is where you should store your connection
string. If your using the free and Brilliant Visual Web Developer, you
should see that a web.config file is created for you when you create a new
website. If your using an existing website without a WEB.CONFIG file you can
create one in VWD or VS.NET by right clicking on your project and clicking
ADD NEW ITEM.

Once you've done this you can add a new connection string in this XML based
document.

Heres the code for the KEY valued pair connection variables:

<appSettings>
<add key="MyConnString"
value="server=localhost;database=Northwind;uid=sa;password=secret;" />
</appSettings>

Now in any subsequent classes or ASPX pages that you create simply call the
connectionstring as:

Dim cn As New
SqlConnection(ConfigurationSettings.AppSettings("MyConnString").ToString())

or in ASP.NET 2.0

Dim cn As New
SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString.ToString())

Hope this helps!

Damian
MCP
 
hmm, the plot thickens. this works initially, but on it fails as attached on
__doPostBack().

any ideas? i've tried adding a couple of additional event handlers (that
call my initialization code), but no luck so far. e.g.

protected override void OnPreRender(EventArgs e) {
initConnectionStrings();
base.OnPreRender(e);
}

cheers,

Tim

Stack Trace:

[InvalidOperationException: The ConnectionString property has not been
initialized.]
System.Data.SqlClient.SqlConnection.PermissionDemand() +852019
System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection
outerConnection) +22
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory) +105
System.Data.SqlClient.SqlConnection.Open() +111
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset,
DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String
srcTable, IDbCommand command, CommandBehavior behavior) +121
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord,
Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior
behavior) +137
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
+83
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments
arguments) +1770
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,
DataSourceViewSelectCallback callback) +17
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +24
System.Web.UI.Control.PreRenderRecursiveInternal() +77
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360
 
Hello Tim,

Have you try to set the connction string in SqlDataSource's Init event or
load event?

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
thanks, Luke, seems like a great idea. once again i didn't know there was
such a thing. haven't learned to think enough in DotNet yet, i fear.

what are my options here? i see from an MSDN document that i can name the
Init Event Handler. but wouldn't it be better to know what the default
handler is (so i can call it before or after my code)?
 
Hello,

What is the "default handler" here? The SqlDataSource's event handler? You
may refer following page to see all of its events we can use:

http://msdn2.microsoft.com/en-us/library/5b6ksy3z.aspx

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
hi, Luke,

very late update. a clue from a colleague led me to the best way to do
exactly what you said: it's easier than i thought.

in the pull-down where one finds Server Objects & Events i found my data
sources. with one of them selected the pull-down to the right shows the
events. i chose load. it inserted roughly this:
protected void SqlDataSource3_Load(object sender, EventArgs e) {
}
and i inserted the code i needed:
SqlDataSource3.ConnectionString = ...

as you predicted this works both on the initial load and with callbacks.

thanks again,

Tim
 
Back
Top