Cyprich.com

Occasionally Relevant Insights from a Software Builder

By

A Better Error Page on the Canada Immigration Site

Tonight during the historic US election, the Canadian Immigration site (http://www.cic.gc.ca/) was down. I’m sure it had nothing to do with Americans trying to leave their country and move to Canada in light of the imminent Trump victory. The following ASP.NET error page was displayed:

Screen Shot 2016 11 08 at 10.59.59 PM

This is a default error page that gets displayed for ASP.NET sites. This is ok, but I prefer something more friendly than this type of page which doesn’t mean much for most people. A custom error page should have information about your website and contact info for the user to let you know the site failed.

The code to redirect errors to a custom page is added to the web.config file (located in the root of the website folder). The following settings refers to Error.aspx in the /ErrorPages folder.

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPages/Error.aspx" />
    </system.web>
</configuration>

By

Using an Apostrophe in a Variable in MSSQL

A user created an account in my web application which had an apostrophe in their e-mail address. It was similar to First_D’Second@website.com. I didn’t think that an apostrophe was a valid character for an e-mail so I didn’t have any code to prevent it from crashing the app when it tried to run. The original code was:

string sql = "SELECT * FROM Users WHERE Email = '" + email + "'";

You can prevent an error by escaping the apostrophe in the email variable, i.e. replacing a single apostrophe with a double one. This can be done in C# with the .Replace function:

string sql = "SELECT * FROM Users WHERE Email = '" + email.Replace ("'", "''") + "'";

The sql string can then be executed without crashing the application.

By

Redirecting ASP.NET Pages

You can redirect an ASP.NET page using one of three methods. The first method is through metatag redirection. If that fails, then it will try to redirect with JavaScript. If that fails, a URL will be displayed that the user can click on to redirect the page.

An HTML 5 only version of this redirect page can be found in the post Redirecting Pages in HTML 5.

— ASP.NET code below —

<%@ Page Language=”C#” %>

<!DOCTYPE html>

<script runat=”server”>
</script>

<html>
<head runat=”server”>
<title>Page Title</title>

<meta http-equiv=”Refresh” content=”0; url=http://www.website.com”>

<!– If the meta tag doesn’t work, try JavaScript to redirect. –>

<script type=”text/javascript”>
window.location.href = “http://www.website.com”
</script>
</head>

<body>
<form id=”form1″ runat=”server”>
<div>
<!– If JavaScript doesn’t work, give a link to click on to redirect. –>

<p>If you are not redirected automatically, click <a href=’http://www.website.com’>here</a>.</p>
</div>
</form>
</body>
</html>

By

Restore Backed Up Database in MS SQL Server Using Transact-SQL

The following code will restore a database from a backup file in MS SQL Server. Normally you would use Management Studio to do this, but this might fail if the database needs to be in single user mode to restore. The code below will restore the database as a single user, then put it back in multi-user mode after its done.

—[ Transact-SQL Code ]——————–

-- Set database as single user and restore from backup.
USE master
ALTER DATABASE <your_database>
 SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE <your_database>
 FROM DISK = 'D:\Databases\YourDatabase.bak'
 WITH REPLACE
GO
-- Set database back to multi user.
ALTER DATABASE <your_database> SET MULTI_USER WITH ROLLBACK IMMEDIATE
GO

By

Create a Custom Help File in uStore

You can define custom help or context sensitive help in uStore by creating an HTML file and entering it in the appropriate text box. This section is found when you login to uStoreAdmin and then do the following steps:

  1. Open the store.
  2. Go to Store Setting.
  3. Select Set Up Store.
  4. Go to the Advanced tab.
  5. In the Storefront Help Info section, you enter name of the help file(s) in Custom Help File or Context Senstive Help.

image
Click image to enlarge.

Its easy to create an HTML help file, but if you use the same file for multiple stores, its a good idea to make this file a redirect page which takes the user to the actual help file. The reason for this is that sometime down the road, you may decide to use a page with a different name, or in my case, the page could be hosted in a Joomla site where the name for a new help file that I create will be different.

If you only have one store, then it doesn’t matter what you name the file since its easy to replace, but if your hosting hundreds of stores it will not be a small task to replace all of the links with a new filename.

A simple solution is to make the help file an .ASPX redirect page. Each store will have the same file and if you create a new help file, the only thing you need to do is modify your .ASPX redirect file

The following page is an an .ASPX file which is saved in your XMPie installation at \XMPie\uStore\App\CustomerApp. Modify the red text to the actual URL of the help text.

—[ Help.aspx ]——————————–

<%@ Page Language=”C#” %>

<!DOCTYPE html>

<script runat=”server”>
</script>

<html>
  <head runat=”server”>
    <title>
uStore Help</title>

    <meta http-equiv=”Refresh” content=”0; url=http://mycms/ustore/help-12102013/>

    <!– If the meta tag doesn’t work, try JavaScript to redirect. –>
    <script type=”text/javascript”>
      window.location.href = “
http://mycms/ustore/help-12102013
    </script>
  </head>

  <body>
    <form id=”form1″ runat=”server”>
      <div>
        <!– If JavaScript doesn’t work, give a link to click on to redirect. –>
        <p>If you are not redirected automatically, click <a href=’
http://mycms/ustore/help-12102013‘>here</a>.</p>
      </div>
    </form>
  </body>
</html>