Create an E-newsletter Signup System with ASP.NET | 2 | WebReference

Create an E-newsletter Signup System with ASP.NET | 2


[prev]

Create an E-newsletter Signup System with ASP.NET [con't]

Writing the Connection Object

Since you need to send the emails from a query, you first need to add the code to connect to your database. Start by adding that code right below where your SMTP object sets your host as shown below:

Write the Subscribe Code

When your visitor fills in appropriate information and clicks subscribe, you need to capture the corresponding information. You could add this code directly to the code behind file, but because you need to add update functionality later, creating a class to handle this would be better. It leads to more modularity in code and lends itself to object-oriented programming.

Create a separate class that will handle this functionality. Right click on your project, and select "add new item" and then class. Type "Newsletter.cs" and click add. Double click the file to open it and add the following at the top of the class file:

In the code above, you import the necessary namespaces (libraries) to work with active database objects (ADO), which is essentially database access and your web configuration file. In your class definition, add the following code:

First, you create a region, so that you can cleary indicate in your code that you're declaring class-level variables. You set the variable to public so that your code behind file will see them, and give them an appropriate data type. Another way of doing class-level variables is setting them to private and then accessing them through public properties. Each way has its positives and negatives.

Next, add the method for inserting the record into the database right below your end region:

In the code above, you do the following: create a connection object and set it to your connection key name that comes from your configuration file. Next, you create a command object, which accepts two parameters: your SQL insert statement and your connection object. You may wonder why on the second part of the insert statement you used the at symbol (@). In ASP.NET, the at @ symbol is a parameter and it helps prevent SQL injection attacks.

After your insert statement, you create parameter variables that tell you what each of your column's data types are. This way, if someone tries to enter an unknown data type in your fields, your application will throw an exception (you'll see how to display this error later in the article). Obviously, your validation should handle this, but this approach provides even more protection for your applications and data, ensuring you get what you expect.

After you add each parameter variable to your command object's parameters collection, you use a try/catch block, open your connection to the database, and call the ExecuteNonQuery method of your command object. You use this method because you don't need to see the results; you're saying just perform the operation. If you can't open the connection or execute the query, you catch the exception. You then dispose (discard) your command object and close your connection object by using its close method in your finally code block. Make sure you save your file before continuing.

In your ASPX code behind, create an instance of the newsletter class as shown below:

In this code above, you simply create an instance (object) of the newsletter class and name it nl.

In your event handler, add the following code:

In the code above, you simply set the properties of your newsletter object to your user form controls, and then you call your insert method. If it's successful, toggle the visibility of your Placeholder controls. Before moving forward, save your file.

Hooking Up the Validation

Currently, if JavaScript is disabled and you preview your form and click subscribe with the form fields empty, the form will submit and you'll get a blank record. In order to get validation working on the server side, simply adjust the code behind event handler you worked on above to this:

In the adjusted code behind event handler, you added an "is valid" check and negated the statement. So, the translation is: "If the page isn't valid, stop execution of code and display your error messages. Otherwise, record the information in the database table."

Save your page and try again. This time, click subscribe while leaving fields empty and make sure the error messages show. Then fill them in individually, and when those are complete, click subscribe and then the information should be recorded.

In summation, ASP.NET's built-in validations controls provide you with instant client-side validation, while your "is valid" check provides your server-side validation ensuring all your controls are valid before allowing the form to submit if JavaScript is disabled.

Handling and Displaying the Exception

Right now, if an exception occurs, your page will show an ugly error message. A user does not need to see this scary warning. What they need is a generic page informing them something went wrong. Fortunately, ASP.NET has a clever way of handling this: custom errors. In your web configuration file, look for <customErrors>. Simply change the default markup with the following:

Make sure you create a HTML page within your solution. The nice part about this implementation is when users throw an exception remotely, they see this generic error page, which you can customize to fit your needs. Meanwhile, you as the developer can step through the exception and fix the problem locally.

Writing the Unsubscribe Code

In your e-mail that gets sent to your subscribers, you need to provide a mechanism to allow your subcribers to unsubcribe from periodic e-mails. The easiest implementation is to provide a link at the bottom of your e-mail that, when clicked, sends your subcribers to an unsubcribe page. From this page, you'll let them enter their e-mail address and, if you find it, you'll change the column in your database table to false. The ASPX page for unsubcribing is already completed, so you'll just work with the code behind and your class file.

Work with Your Class File

Right above your end region for methods, add a method for updating subscribers. Add the following code:

In this code, you added a new query. You set the subscribe column to false (you'll see that shortly) where the e-mail matches that entered by the visitor. Save your class file. In your code behind file, add the following code:

In this code, you set the subscribe field to false and then call the appropriate method. Save your files and preview the results.

Customizing sendemails.aspx

For the purposes of this article, you will walk through the changes you need to make in the mail script from my second sending an e-newsletter with ASP.NET article. Open the code behind file and observe line 47:

You added an introduction to your e-mail. The squiggly brackets ({}) serve as an anchor in string replacement, so that individual e-mails sent have the person's name included. You'll see how string replacement works shortly.

At line 61, hit the enter key once and at line 62, insert this code snippet:

At line 123, hit the enter key twice and at line 125, insert this code snippet:

In line 130:

You adjusted the query to check for a subscription in SQL, checking a boolean field is done by 0 or 1.

In lines 134-136:

In your first while loop, you create a string variable and set it equal to the concatented result of your data reader object that contains the first and last name from your database table.

In lines 137-138:

You simply take the existing variables that hold your HTML or plain text content, and then use the replace function. This function takes two parameters:

  1. The old value you're looking for (in your case {Name})
  2. The replaced value (in your case, your name variable)

Save your file and run the example. In a few minutes, you should receive e-mails with a personal introduction.

Summary

In this article you learned how to create a form allowing users to fill out basic information and choose whether they want to receive periodic e-mails or not. Along the way, you learned the following:

  • Modifying an existing database table
  • Working with bit fields in SQL
  • Validation between the client and server sides
  • Creating validation controls
  • Creating a class file:
    • Creating variables and methods
    • How to prevent SQL injection and working with parameters
  • Create an object of that class in your code behind file
  • How to personalize the e-mail
  • How to handle and display friendly error messages
  • Providing unsubscribe functionality

Take the knowledge you gained in this article and give your subscribers the flexibility they need.

If you have questions, please contact me at the address below:

http://midwestwebdesign.net/tutorials/contact.aspx

Download the files for this article.


Ryan Butler is the founder of Midwest Web Design. His skill sets include HTML, CSS, Flash, JavaScript, ASP.NET, PHP and database technologies.

Original: Apr. 7, 2010


[prev]