October 23rd 2008

Microsoft reporting: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

This error was bugging :) me for a long time until I found this excellent forum discussion:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=541116&SiteId=1

The following points summarize the causes and solutions from the forum and offer some additional help:

1. Check if you have changed the length of any fields in the underlying database table after creating the dataset. If you have, you may need to manually change them in the dataset as well. If you are using Visual Studio 2008, open the xsd file and click on the relevant field name. Ensure the maxlength property matches your new field length.

Check maxlength property

Check maxlength property

2. If you base your dataset off a view rather than a table, and that view contains a primary key field from the underlying table, visual studio may set unique to true for that field. If your view is constructed in such a way that the field is not unique, then the error will occur. In Visual Studio 2008, open the xsd file, click on the field and set unique to false in properties.

There were other causes of the error but these seemed to be the two most common.

Cheers,
Grant

No Comments yet »

October 20th 2008

A better way of handling maxRequestLength exceptions

ASP.NET 2 includes the FileUpload control to make it easier to create pages that allow users to upload files. The default maximum file size is 4096 KB to minimize the potential for denial of service attacks. You can change the maximum file size by editing your configuration files (see for example http://msdn2.microsoft.com/en-US/library/system.web.configuration.httpruntimesection.maxrequestlength(VS.80).aspx and http://support.softartisans.com/kbview_825.aspx).

The problem is that when users attempt to upload a file that is too large, it is difficult to capture and handle the exception that is created. The usual try..catch doesn’t handle the exception because the exception occurs before then. The exception is “System.Web.HttpException: Maximum request length exceeded”. You could of course increase the maximum file size but that defeats the purpose of the limit in the first place.

Here is one solution for handling the exception that is at least nicer than the standard ASP.NET exception message:

1. Create a global.asax file. If you’re using Visual Studio 2005 it will set up a number of common subroutines for you. You need to use Sub Application_Error. Your code should look something like this:

<script runat=”server”>
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim currentException As Exception
currentException = Server.GetLastError.GetBaseException()
Response.Redirect(”/error.aspx?Err=” & Server.UrlEncode(currentException.Message))
End Sub
</script>

The application_Error sub fires as a last resort, in other words, when you haven’t explicitly handled the exception anywhere else in your code.

2. You can now create an error.aspx that displays the exception message (from the querystring). For this exception the message is “Maximum request length exceeded”. You could also test for the message and give users more information on the error.

Please let me know if you have any comments, suggestions, or improvements.

No Comments yet »

October 9th 2008

Creating multiple websites in IIS 6 for student projects

Every term I had to create between 100 and 200 web sites for students in my classes. Here are the steps I used to create those site in IIS 6. The student’s active directory accounts were already set up by our system administrator. I use FrontPage Server Extensions to allow students to transfer files to the server. Sections in italics below need to be replaced with your settings.

1. Create Directories:

md studentdirectory

2. Assign the student user account relevant permissions:

xcacls studentdirectory /T /E /G studentusername:F

3. Create a virtual directory in IIS for each student:

iisvdir /create “IISsitenamestudentvirtualdirectoryname studentdirectory

4. Create a subweb for each student:

“C:\Program Files\Common Files\Microsoft Shared\web server extensions\50\bin\owsadm” -o install -p 1100 -w studentvirtualdirectoryname -u usernameWithSufficientPermissionsOnServer

5. Give the new subweb unique permissions:

“C:\Program Files\Common Files\Microsoft Shared\web server extensions\50\bin\owsadm” -o setperms -p 1100 -w studentsubwebname -i false -u usernameWithSufficientPermissionsOnServer

6. Give the student the author role on the subweb:

“C:\Program Files\Common Files\Microsoft Shared\web server extensions\50\bin\owsadm” -o roleusers -command add -u studentusername -p 1100 -w studentsubwebname -name Author

No Comments yet »

August 7th 2008

z-index in Internet Explorer 7

The z-index implementation in IE 7 can be troublesome when combined with elements that are relatively positioned. My problem occurred on my personal web site on which I have CSS drop down menus that I want to appear over an element with the position:relative declaration. The drop down menu would appear behind the relatively positioned element despite the z-index being set correctly for the drop down menu styles. The page displayed correctly in Firefox. I found a great post at http://www.quirksmode.org/bugreports/archives/2006/01/Explorer_z_index_bug.html that identified the solution.

The solution involves setting the z-index property for any parent elements of the drop down menu in addition to setting it for the drop-down element itself. For example, suppose the navigation menu is in the nav element. The z-index property must be also set for the pageheader element.

<div id=”pageheader”>
  <div id=”nav”>
  </div>
</div>

No Comments yet »

August 6th 2008

Formatting columns in a GridView

Formatting columns in a GridView is relatively straightforward, once you know the appropriate format string.

Here are the steps if you are using Expression Web, Visual Studio, or Visual Web Developer:

  1. Open the Task Pane of your GridView and select Edit Columns
  2. In the Selected Fields Box, select the column you want to edit
  3. Find the HTMLEncode property and set it to false.
  4. Find the DataFormatString property and set it to the appropriate formatstring. The standard format of a format string is {0:format string}

Some examples of format strings are:

{0:c} to format as currency
{0:M-dd-yyyy} to format as a date

For more formatting strings, go to Formatting Types on MSDN.

1 Comment »

Next »

  • Monthly

  • Users

  • Advertising