Last post Nov 05, 2017 03:21 PM by banksidepoet
Nov 04, 2017 03:47 PM|banksidepoet|LINK
I use something like this:
commandString = "SELECT
[phrase], [url] FROM links WHERE active = @activeState";
within a DataReader that uses cmd...
tempText = tempText.Replace((string)rdr["phrase"], "<a href=\"" + (string)rdr["url"] + "\">" + (string)rdr2["phrase"] + "</a>");
The disadvantage is that I have this on every page. The site even has multiple Master Pages so putting it in those wouldn't solve my issue.
I want to put this code (or something that does the same job) in
ONE place in my website. I'm told that anything that goes in ONE place only in a website, goes in Global.asax. Could anyone verify this and possibly help with how to achieve this, code-wise?
Nov 04, 2017 04:53 PM|markfitzme|LINK
No, that's not true. There are certain things that can happen in the global.asax and those things can happen multiple times such as the beginrequest. When using integrated mode then the request will also process for images, css files, etc.. That would be
bad because you have a database hit then for every activity and that isn't cheap.
So, start to think inheritance. Yes, you have multiple pages, but they all inherit from a base class called System.Web.UI.Page. If you create another class that inherits from this, then your pages can in turn inherit from the new class and they all share
the same functionality.
Do keep in mind that you need to ensure your database calls need to be foolproof and efficient. The Replace you are using is highly inefficient and prone to memory leaks. You should be looking at string.Format to format a string as string concatenation is
a notorious memory issue.
You also need to take into account what happens when the field returned by the reader could be a null database value. Right now this code doesn't appear to handle that and could very well blow up causing the page itself to fail loading.
Nov 05, 2017 03:21 PM|banksidepoet|LINK
Inheritance. Obviously. Doh!
Thank you for that. And thanks for the critique of the existing code. Love to learn!