It is very common for people to use <asp:SqlDataSource> to bind controls in ASP.NET web pages. There are many developers who use <asp:SqlDataSource>to bind controls in their aspx pages. It is also prevalent to bind <asp:DropDownList> also using this <asp:SqlDataSource> But is it OK to use this for every single control (let us say all dropdowns) in a page?
Please read On….
Typically this is what happens:
I go to the design mode of aspx page in Visual Studio, drag a <asp:DropDownList>and as soon as I drop it in the web form, Visual Studio shows me a “chick” context menu sitting on the dropdown control titled as “Dropdown Tasks”. I have “Choose Data Source”, “Configure Data Source” among other things. You click on that menu, a nice little wizard comes up and you just keep clicking what you want and voila my dropdown is ready and bound and it is ready to deliver to my client.
So after this exercise, (when I choose a new <asp:SqlDataSource> in the wizard) typically these are the lines that are emitted in my aspx page. [Please read the code lines once here, even if you are very thorough with this to get accustomed to the control names, Etc]
<asp:DropDownList ID=”ddnCountry” runat=”server”DataSourceID=”sdsCountry” SkinID=”ddnBlue”
<asp:SqlDataSource ID=”sdsCountry” runat=”server”
ConnectionString=”<%$ ConnectionStrings:AdventureWorks2008ConnectionString %>”
SelectCommand=”SELECT [CountryRegionCode], [Name] FROM Person.[CountryRegion]”>
Fantastic! I did not write a single line of code and here is my dropdown, ready to be shipped. So all well? I beg not. Let us expand the project and scenes. I have added a total of 200 pages in my project over a period of 3 months and there are 50 different pages which bind the same “Country” info. So in 50 different aspx pages I have the same piece of code repeating.
Now the fun(?!?) begins – My client tells me that sometimes the order of the country is not correct please make sure to order the country names in alphabetical order in “Contact” page. So I change the query that was in there to “SELECT [CountryRegionCode], [Name] FROM Person.[CountryRegion] ORDER BY [Name]“. Job done, I upload the page again and inform my client that the “bug” is fixed. A week passes and my client screams that “I asked you to Order Country Names in alphabetical order, right? Now looks like that is not happening.” I ask him which page and his answer is “Shipping” page.
So you get the point now, right? I did know that “Country” dropdown is present in 50 different pages when the client first complained about it and I was just afraid to touch “50 different” pages to change this and eventually I did not – But today I have no other option than to “touch” 50 pages!
A week passes by and then my DBA informs me that the name of the schema “Person” in the DataBase “AdventureWorks2008” is now changed to “Persons” due to unavoidable reasons! Oops! I have to touch 50 pages again to change the schema name in the query!