473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Nasty DataSourceCotnrol Behavior

I just spent several frustrating hours tracking down a subtle problem involving a simple web page. I'm sharing the solution so as to
spare others a similar experience.

The web page has only three controls, all of which are bound to SqlDataSource controls: two dropdowns and a repeater. However, the
contents of the second dropdown depend upon the selection made in the first dropdown, and the contents of the repeater depend upon
the selections made in both dropdowns.

The problem is this: SqlDataSource controls have a connection string property. If you use a different datasource for development
than you do on the production site, you have to change the connection string at some point in the page lifecycle when it runs in the
production environment or the data may not be retrieved.

This is not an unusual situation, and with the old data adapter/dataset load model I handled it by assigning connection strings in
the Page_Load method.

Unfortunately, SqlDataSource controls don't persist their connection strings..so if you're going to change them you need to do so on
every postback. You may be able to do this in the Page_Load method, but to be safe I opt to do it by overriding the OnInitComplete
method (because I believe event processing takes place immediately after OnInitComplete, and the controls must be properly
configured before event handling takes place).

Personally, I think the lack of persistence of connection strings is a nasty little bug that ought to be squashed by Microsoft ASAP.
Sometimes I get the feeling that Microsoft doesn't test ASP.NET in the typical "development environment is different from production
environment scenario". I say this because there are too many little "bolt on" solutions to the "problems" caused by multiple
environments (e.g., having to rewrite the Web.config file). It would be nice if they did.

- Mark
Feb 28 '07 #1
5 1256
Hi Mark,

At some respect i dissagree because connection string is used only when
getting the data from data source. Having data populated, there is no need to
retrieve it again on every postback since viewstate was designed to store all
the details control needs to rebuild itself without acquiring data again and
again. I understand this case is different as combo boxes depend on each
other, but in most scenarios connection string is fixed and set for
IsPostBack == false.

Best regards
--
Milosz
"Mark Olbert" wrote:
I just spent several frustrating hours tracking down a subtle problem involving a simple web page. I'm sharing the solution so as to
spare others a similar experience.

The web page has only three controls, all of which are bound to SqlDataSource controls: two dropdowns and a repeater. However, the
contents of the second dropdown depend upon the selection made in the first dropdown, and the contents of the repeater depend upon
the selections made in both dropdowns.

The problem is this: SqlDataSource controls have a connection string property. If you use a different datasource for development
than you do on the production site, you have to change the connection string at some point in the page lifecycle when it runs in the
production environment or the data may not be retrieved.

This is not an unusual situation, and with the old data adapter/dataset load model I handled it by assigning connection strings in
the Page_Load method.

Unfortunately, SqlDataSource controls don't persist their connection strings..so if you're going to change them you need to do so on
every postback. You may be able to do this in the Page_Load method, but to be safe I opt to do it by overriding the OnInitComplete
method (because I believe event processing takes place immediately after OnInitComplete, and the controls must be properly
configured before event handling takes place).

Personally, I think the lack of persistence of connection strings is a nasty little bug that ought to be squashed by Microsoft ASAP.
Sometimes I get the feeling that Microsoft doesn't test ASP.NET in the typical "development environment is different from production
environment scenario". I say this because there are too many little "bolt on" solutions to the "problems" caused by multiple
environments (e.g., having to rewrite the Web.config file). It would be nice if they did.

- Mark
Mar 1 '07 #2
Milosz,

Unfortunately, the problem occurs not because of the interaction between controls, but because the ASPNET engine tries to use the
SqlDataSource control as it was configured during development. In other words, this code will fail, even if there is no control
binding to the data source control:

protected void Page_Load( object sender, EventArgs e )
{
if( IsPostBack )
{
}
else
{
// dsrcMatch's connection string was set to something appropriate to the development environment

dsrcMatch.ConnectionString = CorrectConnectionStringForEnvironment
}
}

I found this out when various pages that use the SqlDataSource control blew up on me when I deployed them.

A solution, as I mentioned in the earlier post, is to do this:

protected override void OnInitComplete( EventArgs e )
{
base.OnInitComplete(e);

dsrcMatch.ConnectionString = CorrectConnectionStringForEnvironment
}

This doesn't blow up, because before the data source control gets "activated" in the course of the page load its connection string
is set to the correct value.

You're right about the ViewState holding state information...but every databound page I've ever written needs to access some data
source in the course of its processing in order to put the information into the ViewState (by way of setting control properties,
usually).

Oftentimes that happens after various postbacks, not just on the first postback. For example, consider a page using a Wizard or a
MultiView, each step of which contains databound controls. Not only wouldn't I want to have to databind the controls on the
"invisible" steps, but many times I can't, because I won't know what to fill them with until after the user walks through the first
couple of steps and postbacks.

So I stand by my assertion that it would be much more convenient for the SqlDataSource control to store its connection string in
ViewState or ControlState. Of course, that introduces a security risk, but I bet that could be handled by encrypting the
information.

- Mark

On Wed, 28 Feb 2007 16:23:18 -0800, Milosz Skalecki [MCAD] <mi*****@DONTLIKESPAMwp.plwrote:
>Hi Mark,

At some respect i dissagree because connection string is used only when
getting the data from data source. Having data populated, there is no need to
retrieve it again on every postback since viewstate was designed to store all
the details control needs to rebuild itself without acquiring data again and
again. I understand this case is different as combo boxes depend on each
other, but in most scenarios connection string is fixed and set for
IsPostBack == false.

Best regards
Mar 1 '07 #3
Hi Mark,

Thank you for sharing your experience with the community and your feedback
regarding the DataSource control's ConnectionString property.

Besides your solution to set the ConnectionString in OnInitComplete, you
could also do it in the DataSource control's Init event.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

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

Mar 1 '07 #4
Good morning Mark,

I understand your reasoning, but to be honest i don't find as a problem
because i never use sql data source on my pages as I tend to move all
database access code to separate place. In addtion to what you mentioned -
security is also one of the reasons, plus keeping viewstate as tight as
possible. It's also simple to use <%$ ConnectionString %(i know that not
the case now) Anyway, as i signalised in last reply, I disagree only at some
point.

Best regards
--
Milosz
"Mark Olbert" wrote:
Milosz,

Unfortunately, the problem occurs not because of the interaction between controls, but because the ASPNET engine tries to use the
SqlDataSource control as it was configured during development. In other words, this code will fail, even if there is no control
binding to the data source control:

protected void Page_Load( object sender, EventArgs e )
{
if( IsPostBack )
{
}
else
{
// dsrcMatch's connection string was set to something appropriate to the development environment

dsrcMatch.ConnectionString = CorrectConnectionStringForEnvironment
}
}

I found this out when various pages that use the SqlDataSource control blew up on me when I deployed them.

A solution, as I mentioned in the earlier post, is to do this:

protected override void OnInitComplete( EventArgs e )
{
base.OnInitComplete(e);

dsrcMatch.ConnectionString = CorrectConnectionStringForEnvironment
}

This doesn't blow up, because before the data source control gets "activated" in the course of the page load its connection string
is set to the correct value.

You're right about the ViewState holding state information...but every databound page I've ever written needs to access some data
source in the course of its processing in order to put the information into the ViewState (by way of setting control properties,
usually).

Oftentimes that happens after various postbacks, not just on the first postback. For example, consider a page using a Wizard or a
MultiView, each step of which contains databound controls. Not only wouldn't I want to have to databind the controls on the
"invisible" steps, but many times I can't, because I won't know what to fill them with until after the user walks through the first
couple of steps and postbacks.

So I stand by my assertion that it would be much more convenient for the SqlDataSource control to store its connection string in
ViewState or ControlState. Of course, that introduces a security risk, but I bet that could be handled by encrypting the
information.

- Mark

On Wed, 28 Feb 2007 16:23:18 -0800, Milosz Skalecki [MCAD] <mi*****@DONTLIKESPAMwp.plwrote:
Hi Mark,

At some respect i dissagree because connection string is used only when
getting the data from data source. Having data populated, there is no need to
retrieve it again on every postback since viewstate was designed to store all
the details control needs to rebuild itself without acquiring data again and
again. I understand this case is different as combo boxes depend on each
other, but in most scenarios connection string is fixed and set for
IsPostBack == false.

Best regards
Mar 1 '07 #5
I think I know what you mean about not using sql data source controls too much. Frankly, I find there's too much stuff that takes
place "behind the scenes" for my taste. I prefer to not have databinding take place until I explicitly want it to...but then again,
maybe I'm just stuck in an ASPNET 1.1 rut :).

- Mark

On Thu, 1 Mar 2007 01:29:03 -0800, Milosz Skalecki [MCAD] <mi*****@DONTLIKESPAMwp.plwrote:
>Good morning Mark,

I understand your reasoning, but to be honest i don't find as a problem
because i never use sql data source on my pages as I tend to move all
database access code to separate place. In addtion to what you mentioned -
security is also one of the reasons, plus keeping viewstate as tight as
possible. It's also simple to use <%$ ConnectionString %(i know that not
the case now) Anyway, as i signalised in last reply, I disagree only at some
point.

Best regards
Mar 2 '07 #6

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

Similar topics

1
by: Derek Fountain | last post by:
I've received some input from the user's browser, checked it for unpleasant stuff, and determined that it contains characters I'm not happy with. I'd like to store it or email it to an...
26
by: Michael Strorm | last post by:
Hi! I posted a message a while back asking for project suggestions, and decided to go with the idea of creating an adventure game (although it was never intended to be a 'proper' game, rather an...
0
by: Jonas Smithson | last post by:
<div style="position: relative;> <!-- I'm the parent --> blah blah blah <div style="position: absolute; bottom: 0;> <!-- I'm the child --> blah blah blah </div> </div> The bottom of the...
5
by: Andrew Baker | last post by:
If you have every had the following error when trying to use load a form in the IDE then read on!!! An exception occurred while trying to create an instance of GSL.Windows.Forms.BrowserFormBase....
19
by: Jerry | last post by:
I managed to narrow this down to a very simple expression. try this: private void Bug() { bool b = false; Test(3, (b || b) && b && !b); } private void Works() {
1
by: Dejan Vesic | last post by:
I found nasty "documentation" bug; ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemglobalizationcultureinfoclasstopic.htm claims that proper culture info name for Serbian (Cyrillic) - Serbia...
1
by: james | last post by:
I am creating an connection string at runtime using decryption (crypto libraries). i have a very unpridictable error while assinging the generated connection string to the connection property. ...
1
by: pigeonrandle | last post by:
Hi, I thought i should give a better explanation to my second 'bug' in my previous post (Bugs in VS2003 SP1) because it's taken me ages to figure out what was going on. Basically, one of my...
4
by: George Sakkis | last post by:
I spent several hours debugging some bogus data results that turned out to be caused by the fact that heapq.nlargest doesn't respect rich comparisons: import heapq import random class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
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...

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.