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

Formatting DateTime in .NET

Here's a quick guide to formatting a DateTime objects...

formatting_date_time.doc

ASP.NET 2.0 - Allow Users to Create List of Things in Gridview and Store to Database When Complete

I wanted to display a grid with columns that contained various input fields (text boxes, dropdowns, ect.), and I wanted to allow the user to add as many rows to the grid as they needed, and fill in the values for each item. And when they were done, click a button to save the list to the database. I also wanted to be able to populate the grid via databinding (no iterating).

I figured I needed a persistable datastore so I could re-databind whenever the user added a new row. In the end, I ended up using the gridview itself as the persistable store (since gridviews automatically persist themselves via viewstate), and creating code that would generate a datable from the current state of the gridview. (There might be a better way to do this, but this is the way I did it, and it works.)

create_list_of_items_and_save_to_DB_later.txt

ASP.NET 2.0 - Send an Email to Recipients Listed in web.config

I've found that most of the time, when I have a web app that at some point needs to send an email to a list of users, it is handy to have them listed in the web.config file. Here is how to do it...

send_an_email_to_recipients_listed_in_web.config.txt

ASP.NET 2.0 Automatic Page Redirects

Say you want to temporarily take down a particular page from your site or application. Here's an easy way to do it by making a simple web.config change...

automatic_page_redirects_in_web.config.txt

Wednesday, July 18, 2007

AJAX-tify an existing non-ajax ASP.NET website

Have you got an already existing ASP.NET web site/application that you want to add AJAX functionality to (like using a nifty web control from the ASP.NET AJAX Control Toolkit)? Here's what you need to do to the web.config...

ajaxtify_an_existing_non_ajax_app.txt