FirstContact - Part 1
Untitled Page
This blog is the first in a series where I will explorer the best way to write
a very simple piece of business logic using the current crop Microsoft
technologies starting with C# and ASP.net.
The business process I have selected is what I call "First Contact". This
is the point of initial interaction between a prospect and a business.
I will be looking at various flavours of "First Contact" in this series
starting with a simple "Contact Us" web page.
The rest of this blog demonstrates the most simples and most flawed use of
ASP.net to provide an example how badly things can go wrong!
The example consists of the following elements (source files included at the
end):
-
contactus.htm
- a web page containing the contact form to be completed.
-
addcontact.aspx - an aspx web page which handles adding the contact
details to the Contact database and notifies the customer service department
about the enquiry.
This example implements the required business logic and hence is perfectly
valid in that regard however if your business is running code like this you
should be very, very worried. The challenge is to know if you are running
good code or bad code like this.
In the next blog in this series I will highlight the issues I see with this
example and refactor it to illustrate how it can be improved.
What issues can you see in this example? Please add them to the comments
for this blog.
contactus.htm
<html>
<body>
<formmethod=getaction="addcontact.aspx">
<imgsrc="generiCoLogo.gif">
<br>
<table>
<tr>
<td>
Name
</td>
<td>
<inputname="name"type="text">
</td>
</tr>
<tr>
<td>
Email
</td>
<td>
<inputname="email"type="text">
</td>
</tr>
<tr>
<td>
Phone
</td>
<td>
<inputname="phone"type="text">
</td>
</tr>
<tr>
<td>
Message
</td>
<td>
<textareaname="message"></textarea>
</td>
</tr>
</table>
<inputid="Send"type="submit"value="Send">
</form>
</body>
</html>
addcontact.aspx
<%@ Page Language="C#" %>
<html>
<body>
<%
if (Request["name"] == "" ||
Request["email"] == "" ||
Request["phone"] == "" ||
Request["message"] == "")
{
Response.Write("<h1>Oops!</h1>Please enter name, email, phone and message.<br><br>Click <a href=\"contactus.htm\">here</a> to go back.");
}
else
{
string message = string.Format("Name: {0}\nEmail: {1}\nPhone: {2}\nMessage: {3}",
Request["name"], Request["email"], Request["phone"], Request["message"]);
System.Web.Mail.SmtpMail.Send("nobody@sentient.co.uk","customerservice@sentient.co.uk","FirstContact", message);
string sql = string.Format("INSERT INTO Contacts(name, email, phone, message) VALUES ('{0}','{1}','{2}','{3}')",
Request["name"], Request["email"], Request["phone"], Request["message"]);
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection("Server=localhost;Database=FirstContact;User ID=sa;Password=");
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, connection);
command.Connection.Open();
if (command.ExecuteNonQuery() > 0)
{
Response.Write("<h1>Thank-you</h1>One of our customer service representatives will be in touch with you shortly.");
}
else
{
Response.Write("<h1>Sorry!</h1>There was a problem please <a href=\"contactus.htm\">try again</a>.");
}
connection.Close();
}
%>
</body>
</html>