Welcome to EMC Consulting Blogs Sign in | Join | Help

Merrick Chaffer's Blog

SQL RAISEERROR and SqlCommand.ExecuteReader doesn't throw an .NET exception

http://www.strangelights.com/blog/archive/2004/06/10/148.aspx

Looks like you have to call .NextResult() on a reader before a SQL RAISEERROR will throw an exception in the .NET client code.

This issue manifests itself in the Microsoft web service software factory in the Repository.cs class file, for the implementations for Find and FindOne. The corrected code is posted below for reference.

public List<TDomainObject> Find<TIdentity>(

            ISelectionFactory< TIdentity > selectionFactory,

            IDomainObjectFactory<TDomainObject> domainObjectFactory,

            TIdentity identity)

        {

            List<TDomainObject> results = new List<TDomainObject>();

 

            using (DbCommand command = selectionFactory.ConstructSelectCommand(db, identity))

            {

                using (IDataReader rdr = db.ExecuteReader(command))

                {

                    do

                    {

                        while (rdr.Read())

                        {

                            results.Add(domainObjectFactory.Construct(rdr));

                        }

                    } while (rdr.NextResult());

                }

            }

            return results;

        }

 

public TDomainObject FindOne<TIdentity>(

            ISelectionFactory<TIdentity> selectionFactory,

            IDomainObjectFactory<TDomainObject> domainObjectFactory,

            TIdentity identity)

        {

            TDomainObject result = default(TDomainObject);

            using (DbCommand command = selectionFactory.ConstructSelectCommand(db, identity))

            {

                using (IDataReader rdr = db.ExecuteReader(command))

                {

                    do

                    {

                        if (rdr.Read())

                        {

                            result = domainObjectFactory.Construct(rdr);

                        }

                    } while (rdr.NextResult());

                }

            }

            return result;

        }

 

 

 

Published 17 July 2007 17:32 by merrick.chaffer
Filed under: , ,

Comments

No Comments
Anonymous comments are disabled

This Blog

Syndication

News

Powered by Community Server (Personal Edition), by Telligent Systems