473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
Oct 2 '06 #1
11 4832
you cannot use <%= %with server side controls, and the <%# %is for
binding. in OnPreInit set the value rom the code behind.

-- bruce (sqlwork.com)
"tbh" <fe****@newsgroups.nospamwrote in message
news:eI**************@TK2MSFTNGP02.phx.gbl...
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


Oct 2 '06 #2
tbh
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
you cannot use <%= %with server side controls, and the <%# %is for
binding. in OnPreInit set the value rom the code behind.

-- bruce (sqlwork.com)

Oct 2 '06 #3
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]

"tbh" <fe****@newsgroups.nospamwrote in message
news:eI**************@TK2MSFTNGP02.phx.gbl...
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


Oct 2 '06 #4
tbh
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
Oct 3 '06 #5
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("M yConnString").ToString())

or in ASP.NET 2.0

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

Hope this helps!

Damian
MCP
Oct 3 '06 #6
tbh
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.PermissionDema nd() +852019
System.Data.SqlClient.SqlConnectionFactory.Permiss ionDemand(DbConnection
outerConnection) +22
System.Data.ProviderBase.DbConnectionClosed.OpenCo nnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory) +105
System.Data.SqlClient.SqlConnection.Open() +111
System.Data.Common.DbDataAdapter.FillInternal(Data Set 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.Execut eSelect(DataSourceSelectArguments
arguments) +1770
System.Web.UI.DataSourceView.Select(DataSourceSele ctArguments arguments,
DataSourceViewSelectCallback callback) +17
System.Web.UI.WebControls.DataBoundControl.Perform Select() +149
System.Web.UI.WebControls.BaseDataBoundControl.Dat aBind() +70
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.Ens ureDataBound() +82
System.Web.UI.WebControls.GridView.OnPreRender(Eve ntArgs 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

Oct 4 '06 #7
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 5 '06 #8
tbh
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)?
Have you try to set the connction string in SqlDataSource's Init event or
load event?

Oct 5 '06 #9
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 6 '06 #10
tbh
thanks, Luke, i will try to learn this stuff as soon as i find time. cheers,
Tim
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

Oct 6 '06 #11
tbh
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
Nov 1 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my...
1
by: Arne Garvander | last post by:
How do I set parameters from code for a SqlDataSource? -- Arne Garvander (I program VB.Net for fun and C# to get paid.)
4
by: saturnius | last post by:
Hello, I have to save the data from a gridview as XML file. The code is shown below. Is there no easier way? I would like to access either asp:SqlDataSource (me.SqlDataSource1.something) or...
1
by: Jon Paal | last post by:
how does a querystring value get passed to the selectcommand statement of an asp:SqlDataSource? <asp:SqlDataSource id="propertyDataSource" Runat="Server" ConnectionString="<%$...
6
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I'm trying to implement roles in various components (i.e. using different sets of configuration depending, in this case, on which environment your in). Pretty easy to add an AppSettings...
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
0
by: KUVU | last post by:
I am using FormsAuthentication.SetAuthCookie(txtbox1.text,false) in my login page. In my search.aspx i need to pass this login name to sqldatasource,delete commamd i am using...
1
by: E. Kwong | last post by:
I just want to select records with a certain date field today's date: <asp:SqlDataSource ID="src1" runat="server" ConnectionString="<%$ ConnectionStrings:xyz %>" SelectCommand="SELECT * FROM ...
4
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.