| Subcribe via RSS

Accessing Session Variables from a Class

February 27th, 2009 | Comments Off | Posted in VB.NET

I’ve got a vb.net class file that needed to access the Session object to determine if the logged in user qualified for special product pricing. Session(“user_group”) spits back a nice nasty little message saying “Name ‘Session’ is not declared.” The compiler is right. Its not declared. Since we are working within the bounds of the class, there is no System.Web space by default like the Page object enjoys so to pull back the Session content we simply need to fully qualify where the compiler can find that Session object:

System.Web.HttpContext.Current.Session(“networkname”)

Tags: , ,

Create Excerpt from Description Blurb

February 25th, 2009 | Comments Off | Posted in C#, SQL

So the task at hand is taking a verbose piece of description text and breaking it down to the first 300 characters. Not only are we going to trim this to 300 characters, but we are going to drop off all the text that follows the last period. This will serve as teaser copy on our store page to get our fair reader to click over to read the full description if interested. So our first step will be taking that large block and cutting it down to 300 characters:

This SQL query basically says we want the left 300 characters of the description field, and we are going to store it in a variable we call excerpt. Now comes the tricky part of dealing with the text after the period. I’ve created a console application in Visual Studio to help whittle down the results. The following sets up our initial query:

Now we need to loop through that results set so we can pair down this data.

The program doesn’t need to worry about any descriptions that already end in a period or those ending as a url string so let’s skip them.

Here’s where we identify where the last period in our content block resides.

Tags: , ,

Check Web Page Updates Through Screen Scrape

February 24th, 2009 | Comments Off | Posted in ASP.NET, C#

I’ve got a fair share of websites that draw data from a event promoter’s website. I’d originally done research on utilizing their web services model, but was severely disappointed with their implementation. That left me with two essential problems I needed to solve:

1) I needed to know when certain events were updating so I could keep my websites up to date.

2) I needed to be able to parse through the raw html and parse out the relevant data to generate an XML file that my GridView can pull to display the relevant data to my vistors.

The first task was to build a Console App that I could setup as a Scheduled Task to run nightly to tell me which pages had updated. So up Visual Studio goes. I’ve chosen not to hard code the page values so I can easily add additional pages to check in the future. I’ve stored these values in a text file called parsePages.txt as follows:

yahoo,http://www.yahoo.com google,http://www.google.com

These values could be just as easily stored in a database or an xml document. Next, I go about pulling this data into the program so I know which pages I need to parse through. I setup the regular expression to split up the values into an ArrayList from parsePages.txt so I can start making my checks.

Now that I have my list of files to parse through, I’m going to loop through each — checking to see if the file exists then executing the parse routine.

Here is where we do our file verification. We are using two text files for each value — one to house our current content and one for the previous days content. This way we can see what changes, if any, have been made since the previous day. If for some reason either the main file or the alt file doesn’t exist, we want to create it. This is handy for new events we’ve just added to the list.

So far we’ve done a lot of work, but we still haven’t gotten to the meat of what we’re trying to do here. Well never fear because here comes the parse routine. So we pass in the reference and the url into the ScreenScrape method. We use the System.Net.WebRequest object to create our url and the WebResponse to instantiate it.

We spit the html contents of the page into a StreamReader then do a little cleanup to strip off the header and footer html that we aren’t concerned with.

Next, we need to compare the main text file with the alt text file to identify which one was written to most recently so we can overwrite the alternate file with the html we were chewing on in the StreamReader.

Now if we found any change between the old and the new file, we need to know about it. If the file lengths don't match, we write a line out to the console saying an update was found.

Tags: , , , , ,

SQL IF…ELSE: CASE Statement

February 23rd, 2009 | Comments Off | Posted in SQL

I was looking for a way to approach my SQL query that would work similar to a traditional if – then conditional in c#. So I came across SQL CASE statements, and they are exactly what I was looking for. I can do a test conditional based on one of the values passed back from the query to conditionally set a value. An example with multiple conditions of this follows:

Basically, we are saying that when the title of our book is equal to ‘ASP.NET Programming’ and there is no set price for the current year, use last year’s price. For everything else, just use this year’s price. We define this new custom variable as ‘Pricing’.

Reference Article: http://www.databasejournal.com/features/mssql/article.php/3288921/T-SQL-Programming-Part-5—Using-the-CASE-Function.htm

Tags: , ,

Looking for NULL Values in SQL

February 23rd, 2009 | Comments Off | Posted in General, SQL

After spending much too long trying to figure out why a SQL CASE statement wasn’t correctly evaluating the second condition in this statement:

I realize rather foolishly it is the way I am comparing the value to NULL. SQL is always IS NULL and not = NULL.

Tags: , ,

Storing State in AJAX/Javascript: Cookie Based

February 23rd, 2009 | Comments Off | Posted in AJAX, Javascript

In searching for the best way to persist state in my AJAX application, I found writing to a cookie to be the best alternative. The following is an example on how to implement cookie based state management in javascript/AJAX:

Reference Article: http://www.w3schools.com/JS/js_cookies.asp

Tags: , ,

Introduction to ASP.NET Programmer

February 21st, 2009 | Comments Off | Posted in General
ASP.NET Programmer is a site built to store programming code samples for daily challenges we come across that will no doubt surface over and over again as well as an avenue to feature our projects we’ve built and systems we manage to highlight our consulting services. Regardless, we hope to provide much value in an overall webspace that seems loaded down with fluff. Enjoy!