NHibernate - Strange Generic ADO Exception - Asked By Christopher Baker on 23-May-12 12:14 AM

I am trying to call an object.Update() method from my data access DLL. One of my sites throws an error like this:

NHibernate.Exceptions.GenericADOException was unhandled by user code
  Message=could not update: [dbCustPortal.dal.domain.CustomerContact#20][SQL: UPDATE tblCustomerContacts SET ContactName = ?, CompanyName = ?, Address1 = ?, Address2 = ?, City = ?, State = ?, ZIP = ?, Phone = ?, EmailAddress = ?, CustomerID = ?, CreatedDate = ?, LUDate = ? WHERE RecID = ?]
  Source=NHibernate
  SqlString=UPDATE tblCustomerContacts SET ContactName = ?, CompanyName = ?, Address1 = ?, Address2 = ?, City = ?, State = ?, ZIP = ?, Phone = ?, EmailAddress = ?, CustomerID = ?, CreatedDate = ?, LUDate = ? WHERE RecID = ?

Whereas another site that uses the same DLL can call the same method successfully. Does this mean that there's something I don't understand about NHibernate and the session object?

Here's how I'm calling the method:

Dim updatedCustomerContact As CustomerContact = Session("CustomerContact")
updatedCustomerContact.ContactName = RTrim(txtContactName.Text)
updatedCustomerContact.CompanyName = RTrim(txtCompanyName.Text)
updatedCustomerContact.Address1 = RTrim(txtAddress1.Text)
updatedCustomerContact.Address2 = RTrim(txtAddress2.Text)
updatedCustomerContact.City = RTrim(txtCity.Text)
updatedCustomerContact.State = ddlState.SelectedValue
updatedCustomerContact.ZIP = RTrim(txtZIP.Text)
updatedCustomerContact.Phone = RTrim(txtPhone.Text)
updatedCustomerContact.EmailAddress = RTrim(txtEmail.Text)
updatedCustomerContact.LUDate = Date.Now
updatedCustomerContact.UpdateRecord()

and here's the method in the DAL DLL:

Public Sub UpdateRecord() Update() End Sub

I can post more code if needed; just wondering if anyone has seen this kind of behavior before and can tell me where to start looking. Thanks all
Mark Ongolo replied to Christopher Baker on 23-May-12 12:08 AM

I think  the message is trying to point you to the problematic object causing the error. By the way is this a web application, you should use Merge instead of update......


Jitendra Faye replied to Christopher Baker on 23-May-12 12:22 AM
Hi ,

There are two possibilities for this-

1. Identity column problem.
2. Invalid Column mapping.


It may be possible that you column mapping is invalid check column name while making column mapping.
Somesh Yadav replied to Christopher Baker on 23-May-12 12:27 AM

When NHibernate throws an exception like this, it includes the inner exception thrown by the ADO provider, which will, 99% of the time, say exactly what was wrong with the SQL statement that failed. The inner exception is MUCH more helpful; NH just wraps it in something more generic so you can catch the generic exception instead of all possible provider-specific exceptions thrown by the actual ADO.NET layer NH uses to transmit SQL.

From what you've shown, my number-one suspicion is that the columns in the DB are not named exactly like your properties, and you are not specifying different column names in the mapping. If you only specify the property, the property and column names must be a match according to the DB's collation (basically meaning the only possible difference could be case and only in a case-insensitive DB).

One other thing, not related to the error: Your AddSet() function doesn't allow for external transaction control, because each operation is not just its own transaction, but its own session. This is generally a bad idea, because if Set 1 and Set 2 must both be added, Set 1 is inserted, then Set 2 fails, Set 1 will still be in the DB. Usually, you have to be able to tell NHibernate that multiple operations should be all-or-nothing, by allowing the code to obtain some Transaction or UnitOfWork reference that it can pass into various repository methods to identify a transactional context.

Goniey N (Mr. G) replied to Christopher Baker on 23-May-12 12:30 AM
i think there will be problem with your hibernate configuration file..

just check it out that is there any problem or not?
Asked By Christopher Baker on 23-May-12 08:39 AM
I included the inner exception in my original post. It is:

 Message=could not update: [dbCustPortal.dal.domain.CustomerContact#20][SQL: UPDATE tblCustomerContacts SET ContactName = ?, CompanyName = ?, Address1 = ?, Address2 = ?, City = ?, State = ?, ZIP = ?, Phone = ?, EmailAddress = ?, CustomerID = ?, CreatedDate = ?, LUDate = ? WHERE RecID = ?]
  Source=NHibernate
  SqlString=UPDATE tblCustomerContacts SET ContactName = ?, CompanyName = ?, Address1 = ?, Address2 = ?, City = ?, State = ?, ZIP = ?, Phone = ?, EmailAddress = ?, CustomerID = ?, CreatedDate = ?, LUDate = ? WHERE RecID = ? 

The odd thing is that if I step into the DAL DLL, the object is fully populated. Said another way, those question marks should be filled with object properties. I don't understand why they are not.
Christopher Baker replied to Somesh Yadav on 23-May-12 09:25 AM
The property names are identical to the DB field names, including case.

I think that maybe you are on to something with your discussion of each operation being its own session. Can you please explain a little further?
Sreenath Kalahasti replied to Christopher Baker on 23-May-12 05:25 PM
Hi Chris,

Is there a way you can send me the inner exception. 

Inner exception populates the details of the actual error.

Thanks
Sree
Christopher Baker replied to Sreenath Kalahasti on 23-May-12 05:50 PM
The inner exception is in my most recent post.

Message=could not update: [dbCustPortal.dal.domain.CustomerContact#20][SQL: UPDATE tblCustomerContacts SET ContactName = ?, CompanyName = ?, Address1 = ?, Address2 = ?, City = ?, State = ?, ZIP = ?, Phone = ?, EmailAddress = ?, CustomerID = ?, CreatedDate = ?, LUDate = ? WHERE RecID = ?]
  Source=NHibernate
  SqlString=UPDATE tblCustomerContacts SET ContactName = ?, CompanyName = ?, Address1 = ?, Address2 = ?, City = ?, State = ?, ZIP = ?, Phone = ?, EmailAddress = ?, CustomerID = ?, CreatedDate = ?, LUDate = ? WHERE RecID = ? 
Sreenath Kalahasti replied to Christopher Baker on 23-May-12 06:03 PM
Ok.. I am guessing this is because of Application Pool settings in your other server. Can you please check if the Isolation level is ReadOnly? ReadCommitted? ReadUncommitted and change it accordingly.

Thanks,
Sree
Christopher Baker replied to Sreenath Kalahasti on 24-May-12 08:28 AM
The error is occurring during testing on my PC, using the VS Development Server. How does your question fit in that context?

It also happens in production, but that seems to me to be less relevant since I am having the problem in testing. 
[)ia6l0 iii replied to Christopher Baker on 28-May-12 01:17 PM
I have four suggestions.

a) You should look at the Inner Exception for the exact cause of the erroneous behavior. That will tell why the "update" failed. 

b) One reason that I could immediately think of is the DATA - that you used with the NHibernate queries. The data should be different from the machines where it works. Examine it, it could be as simple as the datatype mismatch, or lengthier inputs for fields which are fixed to be of certain length. 

c) If that does not sound helpful, run the SQL Profiler on the database, and figure out the Update Query that executes. Grab the query that runs and run it in the Query Analyzer. It should then be easy to figure out the reason for failure. 

d) Moreover, if you are working with sessions - you should override the Equals and GetHashcode methods.

Hope one of these help.