Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Gridding My Teeth

    CodeSOD: Gridding My Teeth

    June 12, 2025

    Dan‘s co-workers like passing around TDWTF stories, mostly because seeing code worse than what they’re writing makes them feel less bad about how often they end up hacking things together.

    One day, a co-worker told Dan: “Hey, I think I found something for that website with the bad code stories!”

    Dan’s heart sank. He didn’t really want to shame any of his co-workers. Fortunately, the source-control history put the blame squarely on someone who didn’t work there any more, so he felt better about submitting it.

    This is another ASP .Net page, and this one made heavy use of GridView elements. GridView controls applied the logic of UI controls to generating a table. They had a page which contained six of these controls, defined like this:

    <asp:GridView ID="gvTaskMonth1" runat="server" CssClass="leadsGridView" AutoGenerateColumns="False" OnRowDataBound="gvTaskMonth1_RowDataBound"> ... </asp:GridView>
    
    <asp:GridView ID="gvTaskMonth2" runat="server" CssClass="leadsGridView" AutoGenerateColumns="False" OnRowDataBound="gvTaskMonth1_RowDataBound"> ... </asp:GridView>
    
    <asp:GridView ID="gvTaskMonth3" runat="server" CssClass="leadsGridView" AutoGenerateColumns="False" OnRowDataBound="gvTaskMonth1_RowDataBound"> ... </asp:GridView>
    

    The purpose of this screen was to display a roadmap of coming tasks, broken up by how many months in the future they were. The first thing that leaps out to me is that they all use the same event handler for binding data to the table, which isn’t in-and-of-itself a problem, but the naming of it is certainly a recipe for confusion.

    Now, to bind these controls to the data, there needed to be some code in the code-behind of this view which handled that. That’s where the WTF lurks:

    /// <summary>
    /// Create a roadmap for the selected client
    /// </summary>
    
    private void CreateRoadmap()
    {
    	for (int i = 1; i < 7; i++)
    	{
    		switch (i)
    		{
    			case 1:
    				if (gvTaskMonth1.Rows.Count > 0)
    				{
    					InsertTasks(gvTaskMonth1, DateTime.Parse(txtDatePeriod1.Text), "1");
    				}
    				break;
    			case 2:
    				if (gvTaskMonth2.Rows.Count > 0)
    				{
    					InsertTasks(gvTaskMonth2, DateTime.Parse(txtDatePeriod2.Text), "2");
    				}
    				break;
    			case 3:
    				if (gvTaskMonth3.Rows.Count > 0)
    				{
    					InsertTasks(gvTaskMonth3, DateTime.Parse(txtDatePeriod3.Text), "3");
    				}
    				break;
    			case 4:
    				if (gvTaskMonth4.Rows.Count > 0)
    				{
    					InsertTasks(gvTaskMonth4, DateTime.Parse(txtDatePeriod4.Text), "4");
    				}
    				break;
    			case 5:
    				if (gvTaskMonth5.Rows.Count > 0)
    				{
    					InsertTasks(gvTaskMonth5, DateTime.Parse(txtDatePeriod5.Text), "5");
    				}
    				break;
    			case 6:
    				if (gvTaskMonth6.Rows.Count > 0)
    				{
    					InsertTasks(gvTaskMonth6, DateTime.Parse(txtDatePeriod6.Text), "6");
    				}
    				break;
    		}
    	}
    }
    

    Ah, the good old fashioned loop-switch sequence anti-pattern. I understand the motivation: “I want to do the same thing for six different controls, so I should use a loop to not repeat myself,” but then couldn’t quite figure out how to do that, so they just repeated themselves, but inside of a loop.

    The “fix” was to replace all of this with something more compact:

    	private void CreateRoadmap()
    	{
    		InsertTasks(gvTaskMonth1, DateTime.Parse(txtDatePeriod1.Text), "1");
    		InsertTasks(gvTaskMonth2, DateTime.Parse(txtDatePeriod2.Text), "2");
    		InsertTasks(gvTaskMonth3, DateTime.Parse(txtDatePeriod3.Text), "3");
    		InsertTasks(gvTaskMonth4, DateTime.Parse(txtDatePeriod4.Text), "4");
    		InsertTasks(gvTaskMonth5, DateTime.Parse(txtDatePeriod5.Text), "5");
    		InsertTasks(gvTaskMonth6, DateTime.Parse(txtDatePeriod6.Text), "6"); 
    	}
    

    That said, I’d recommend not trying to parse date times inside of a text box inside of this method, but that’s just me. Bubbling up the inevitable FormatException that this will generate is going to be a giant nuisance. It’s likely that they’ve got a validator somewhere, so it’s probably fine- I just don’t like it.

    [Advertisement]
    Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleFlaws in Weidmueller IE-SR-2TX Routers Allow Remote Root Access!
    Next Article Schemes – create syntax highlighting schemes

    Related Posts

    News & Updates

    The best CRM software with email marketing in 2025: Expert tested and reviewed

    July 22, 2025
    News & Updates

    This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-47575 – Mojoomla School Management SQL Injection

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4885 – iSourcecode Sales and Inventory System SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-5913 – “PHPGurukul Vehicle Record Management System SQL Injection Vulnerability”

    Common Vulnerabilities and Exposures (CVEs)

    Microsoft quietly implies Windows has LOST millions of users since Windows 11 debut — are people really abandoning ship?

    News & Updates

    Highlights

    CVE-2025-49630 – Apache HTTP Server mod_proxy_http2 Denial of Service Vulnerability

    July 10, 2025

    CVE ID : CVE-2025-49630

    Published : July 10, 2025, 5:15 p.m. | 2 hours, 3 minutes ago

    Description : In certain proxy configurations, a denial of service attack against Apache HTTP Server versions 2.4.26 through to 2.4.63 can be triggered by untrusted clients causing an assertion in mod_proxy_http2.

    Configurations affected are a reverse proxy is configured for an HTTP/2 backend, with ProxyPreserveHost set to “on”.

    Severity: 0.0 | NA

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-4571 – GiveWP – Donation Plugin and Fundraising Platform Unauthenticated Data Disclosure and Modification Vulnerability

    June 19, 2025

    CVE-2025-30664 – Zoom Workplace App Path Traversal Vulnerability

    May 14, 2025

    CVE-2025-47860 – Apache HTTP Server Unvalidated User Input

    May 13, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.