Debugging ADO.NET Datatables

Do you ever had the famous error message :

“Failed to enable constraints. One or more rows  contain values violating non-null, unique, or foreign-key  constraints.”

when using ADO.NET Datasets ?

Tthis is an incredibly useful message isn’t it ?

So what can you do to spare you some headaches ? Retrieve a more useful message sure !

You have to know that a Datatable object have a “HasError” property, this property allows you to know if the datatable has one ore more rows in error.

When HasError is true, you can retrieve the rows havin an error with the DataTable’s “GetErrors()” method wich return an array with all datarow having an error.

The “in errror” rows can inform you about errors with the “RowError” property.

Here is a simple helper class which can be used to debug a single DataTable or an entire DataSet:

using System;
using System.Data;

public class DataSetErrorRevealer
{

   /// <summary>
   /// Get errors on rows of a DataTable
   /// </summary>
   /// <param name="errorDataTable">The DataTable to be checked</param>
   public static void RevealTableErrors(DataTable errorDataTable)
   {
      if (errorDataTable == null)
      {
         Console.WriteLine("The provided DataTable object is null");
         return;
      }

      if (errorDataTable.HasErrors)
      {
         DataRow[] rowsInError = errorDataTable.GetErrors();
         if (rowsInError.Length <= 0)
         {
            Console.WriteLine("There is no problem with table {0}", errorDataTable.TableName);
         }
         else
         {
            foreach (DataRow errorRow in rowsInError)
            {
               Console.WriteLine(errorRow.RowError);
            }
         }
      }
   }

   /// <summary>
   /// Get errors on row for all tables of a DataSet
   /// </summary>
   /// <param name="errorDataSet">The Dataset to be checked</param>
   public static void RevealDataSetErrors(DataSet errorDataSet)
   {
         if (errorDataSet == null)
         {
            Console.WriteLine("The provided DataSet is null");
            return;
         }

         foreach (DataTable errorTable in errorDataSet.Tables)
         {
            RevealTableErrors(errorTable);
         }
      }
   }

Use it this way :


   TestDataSet dataset = new TestDataSet();
   try
   {
      using (SqlConnection sqlCon = new SqlConnection("MyConnectionString"))
      {
         SqlCommand sqlComm = sqlCon.CreateCommand();
         sqlComm.CommandType = CommandType.StoredProcedure;
         sqlComm.CommandText = "Select Stored procedure";
         SqlDataAdapter sqlDa = new SqlDataAdapter(sqlComm);
         sqlDa.Fill(dataset);
      }
   }
   catch (Exception ex)
   {
   // Will provide the error message :
   // "Failed to enable constraints. One or more rows
   // contain values violating non-null, unique,
   // or foreign-key  constraints."
   Console.WriteLine("Error : {0}", ex);

   // will provide a more helpful message
   DataSetErrorRevealer.RevealDataSetErrors(dataset);
   }

If you prefer, you can use an extension method :


using System.Data;
using System;

public static class DataSetExtensions
{
   public static void RevealErrors(this DataSet errorDataSet)
   {
      if (errorDataSet == null)
      {
         Console.WriteLine("The provided DataSet is null");
         return;
      }

      foreach (DataTable errorTable in errorDataSet.Tables)
      {
         errorTable.RevealErrors();
      }
   }

   public static void RevealErrors(this DataTable errorDataTable)
   {
      if (errorDataTable == null)
      {
         Console.WriteLine("The provided DataTable object is null");
         return;
      }

      if (errorDataTable.HasErrors)
      {
         DataRow[] rowsInError = errorDataTable.GetErrors();
         if (rowsInError.Length <= 0)
         {
            Console.WriteLine("There is no problem with table {0}", errorDataTable.TableName);
         }
         else
         {
            foreach (DataRow errorRow in rowsInError)
            {
               Console.WriteLine(errorRow.RowError);
            }
         }
      }
   }
}

use :


catch (Exception ex)
{
   // will provide an helpful message
   dataset.RevealErrors();
}

Hope this helps !

using System;
using System.Data;public class DataSetErrorRevealer
{

/// <summary>
/// Get errors on rows of a DataTable
/// </summary>
/// <param name=”errorDataTable”>The DataTable to be checked</param>
public static void RevealTableErrors(DataTable errorDataTable)
{
if (errorDataTable == null)
{
Console.WriteLine(“The provided DataTable object is null”);
return;
}

if (errorDataTable.HasErrors)
{
DataRow[] rowsInError = errorDataTable.GetErrors();
if (rowsInError.Length <= 0)
{
Console.WriteLine(“There is no problem with table {0}”, errorDataTable.TableName);
}
else
{
foreach (DataRow errorRow in rowsInError)
{
Console.WriteLine(errorRow.RowError);
}
}
}
}

/// <summary>
/// Get errors on row for all tables of a DataSet
/// </summary>
/// <param name=”errorDataSet”>The Dataset to be checked</param>
public static void RevealDataSetErrors(DataSet errorDataSet)
{
if (errorDataSet == null)
{
Console.WriteLine(“The provided DataSet is null”);
return;
}

foreach (DataTable errorTable in errorDataSet.Tables)
{
RevealTableErrors(errorTable);
}
}
}

Incoming search terms:

  • datatable geterrors
  • datatable haserrors
  • debugging You may use these HTML tags and attributes -captcha

FreeImage and x64 projects : Yes you can !

A few words about FreeImage

According to the website, FreeImage (http://freeimage.sourceforge.net) is an Open Source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF and others as needed by today’s multimedia applications.

FreeImage is easy to use, fast, multithreading safe, compatible with all 32-bit versions of Windows, and cross-platform (works both with Linux and Mac OS X).

FreeImage is written in C++ but can be used in .NET by using a wrapper (which is provided on the site in the download section)

Using FreeImage in a .NET project

To use FreeImage with a .NET project, you have to include the native C++ library (FreeImage.dll) in the same folder as the executable or in a folder contained in the PATH variable (“%WINDIR%\System32” can be a good choice).
A good way to include the FreeImage dll in the executable’s folder is to add it to your visual studio projet, and set the Build Action property to “none” and the Copy to Output Directory property to “Copy always”

image

You have to add a reference to the wrapper library too, which is “FreeImageNET.dll”.

Now, using the library is easy.

Add an using directive:

using FreeImageAPI;

Check if the library is available :

if (!FreeImage.IsAvailable())
{
   Console.WriteLine("FreeImage.dll seems to be missing. Aborting");
   return;
}

Do something with your pictures:


FIBITMAP myImage= new FIBITMAP();
myImage= FreeImage.Load( [parameters] );

// some smart actions

Don’t forget to dispose your images when finished to use them

if (!myImage.IsNull
{
   FreeImage.Unload(myImage);
}
myImage.SetNull();

Well, this is nice but it still has a problem with that library (there is always one):

And with a x64 project ?

The executable you build has to be the same architecture as the library. The library is x86 so you can use it for win32 applications. What if you have (for some reasons) to build an x64 application?

In that case, you have to rebuild the library to target X64 architectures.

Here is how to do that with Visual Studio 2010 (I assume that it’s more or less the same procedure with other visual studio versions).

Grab the latest FreeImage sources on  the download site (go to the http://freeimage.sourceforge.net/download.html page and click to the link in the Source Distribution section. Once you have the sources, unzip the archive (obvious but…)

In the FreeImage folder there is a visual studio 2005 and a visual studio 2003 solutions, pick the 2005.

image

Convert the solution

image
image
image
image
image

Notice the compilation configuration

image

By default, Win32 is set : we don’t want that !

Go to the Configuration Manager

image
image

As you can see, all FreeImage libs are set to target the Win32 platform, we want 64 bits !

Add a new solution platform

image

Choose to target the x64 platform

image

Just select x64 then click OK

image

Close the manager

Build the solution (keyboard shortcut : ctrl-shift-B).

image

And at this point you will see that there is a problem, yes, you don’t really think it would be that easy ;-)

image

It seems that the compilation encounters a problem especially with the “LibOpenJpeg” project

So, to be sure, try to compile the library one at the same time

image

We see that it is the LibOpenJPEG the big source of problems :

image

A quick Google search allows to find a post on the Google user group for OpenJPEG that helps:
http://groups.google.com/group/openjpeg/browse_thread/thread/64b89cdb1a44bb3b/fd4db2f74c76db87?lnk=gst&q=x64#fd4db2f74c76db87

The problematic file is the “opj_include.h” wich use a keyword non supported by the x64 architecture (the “_asm” keyword if you want to know. It’s because the c++ compiler does not support inline x64 assembly)

image

We have to change :

/* MSVC does not have lrintf */
#ifdef _MSC_VER
static INLINE long lrintf(float f){
 int i;

 _asm{
 fld f
 fistp i
 };

 return i;
}
#endif

to

/* MSVC does not have lrintf */
#ifdef _MSC_VER
static INLINE long lrintf(float f){
#ifdef _M_X64
	return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f));
#else
	int i;

	_asm{
		fld f
		fistp i
	};

	return i;
#endif
}
#endif

Now compile

image

The project compiles pretty well, there is some remaining warnings . . . but it works.

Now you can use that library (mine can be downloaded at http://www.sambeauvois.be/Demos/FreeImage/x64/FreeImageX64.zip) in your x64 applications

Attention anyway : the library will be slower !

Additional links :

FreeImage download page : http://freeimage.sourceforge.net/download.html
FreeImage API documentation: http://freeimage.sourceforge.net/fnet/Index.html

/* MSVC does not have lrintf */

#ifdef _MSC_VER

static INLINE long lrintf(float f){

#ifdef _M_X64

return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f));

#else

int i;

_asm{

fld f

fistp i

};

return i;

#endif

}

#endif

Incoming search terms:

  • NULL
  • FreeImageandx64projects:Yesyoucan!
  • freeimage
  • freeimage x64
  • freeimage net
  • freeimage 64
  • freeimage vs2010
  • freeimage dll
  • FreeImage dll x64
  • freeimage 64bit