Gregg's .Net HowTooz

Tuesday, July 24, 2007

.Net - Simplify Your SQL Data Access

Most of the time, I prefer to access and update SQL data from my ASP.NET apps programmatically (as opposed to declaratively using data source controls). When I do use data source controls (because there deffinitely is a place for them), I prefer to use the ObjectDataSource control, and use a class defined within the data access layer of my app. In either case, it usually involves typing a lot of data access code (ADO.NET).

I've tried various ADO.NET wrapper libraries out there (such as Microsoft's Data Access Application Block), but I wanted something even simpler that required even less code. So I created the Simplification.Data class library...

Simplification.Data_Usage.doc

Simplification.Data.zip

Friday, July 20, 2007

ASP.NET 2.0 - Catch an Error - Send an Email

When one of the apps I have developed throws an exception, I want to be notified, and get all the details about the error that ocurred. I like using the following code to accomplish this, because the email is simple yet detailed.

It starts off with the time it occured, followed by the logged on user, followed by the browser version being used. Then it will list all errors going up the exception chain (not just the last one), showing the details of each exception and the stack trace of each one.

catch_an_error_send_an_email.txt

Working with SAP Dates

When working with the SAP .Net Connector, dates come out and go into SAP as strings with the format yyyyMMdd.

Here are some code examples for working with Dates between SAP and .NET...

working_with_sap_dates.txt

How To Retrieve Data from SAP via a BAPI in a .NET Application

Accessing data from SAP within a .NET application (ASP.NET web app, web service, console app, or whatever) is easy using the SAP .NET Connector. Here is version 2.0...

SAPDotNetConnectorSetup_20.zip

Once you have it installed, just follow my quick guide, and you'll be on your way to getting data out of SAP...

SAP_DOTNET_Connector_quick-start.txt

ASP.NET - Encrypt QueryString Variables

If you have a web app that has pages that display database data and that retrieves that data using a primary key that is passed to the page via a querystring variable like this...
http://mywebserver/MyWebApp/GetItem.aspx?ID=12345
... then you have to consider the fact that a user could very easily change the ID, and retrieve some other data... data that they perhaps should not be looking at.

An easy way to circumvent this is to simply encrypt the ID before appending it to any generated URLs, and de-encrypt it in GetItem.aspx, so that your urls now look like this...
http://mywebserver/MyWebApp/GetItem.aspx?ID=e158f349b, making it very difficult to near impossible (or at least not obvious) for then to simply change a number and get some other data. Here's some code to get it done...

encrypted_querystring_variables.txt

Dynamically Generate an Outlook Reminder Using ASP.NET

There was a project I had worked on where the requirement was to allow users to sign up for an event (an online scheduler). They chose an available time slot, and then were sent an email confirmation of the time slot they had signed up for. After it was done, I thought it would be nice if from the confirmation email, the user could click on a link and it would create a reminder for them in Microsoft Outlook. My solution was to create another page in my web app, which would take an ID as a querystring variable that represented the timeslot the user had signed up for, and return a vCalendar File (.vcs). So in the generated email confirmation, at the bottom I had a link to a page like this... http://MyIntranet/Scheduler/vCalendarFile.aspx?ID=12345.

Here is the code needed to make a page like vCalendarFile.aspx...

vCalendarFile_generation.txt

Thursday, July 19, 2007

Formatting Numeric Values in .NET

Here's a quick guide to formatting numeric values...

formatting_numeric_values.doc