-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Need Help: Incorrect syntax near the keyword .....
PostPosted: Wed Nov 22, 2006 11:44 am 
Newbie

Joined: Tue Mar 21, 2006 3:52 pm
Posts: 5
Location: Atlanta, GA
I keep getting this error and I can't find the problem. I need another pair of eyes on this.

The error:

Quote:

Incorrect syntax near the keyword 'Order'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'Order'.

Source Error:


Line 32: ITransaction transaction = session.BeginTransaction();
Line 33:
Line 34: Customer customer = (Customer)session.Load(typeof(Customer), customerId);
Line 35:
Line 36: transaction.Commit();


Source File: C:\My Visual Studio Projects\CustomerExample\CustomerExample\BLL\CustomerFacade.cs Line: 34

Stack Trace:


[SqlException (0x80131904): Incorrect syntax near the keyword 'Order'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +95
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +82
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +346
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +3244
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +52
System.Data.SqlClient.SqlDataReader.get_MetaData() +130
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +371
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1119
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +334
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +45
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +162
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +35
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() +29
NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd) +92
NHibernate.Loader.Loader.GetResultSet(IDbCommand st, RowSelection selection, ISessionImplementor session) +238
NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies) +354
NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies) +89
NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object[] ids, IType[] types) +73
NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type) +99
NHibernate.Loader.OneToManyLoader.Initialize(Object id, ISessionImplementor session) +35
NHibernate.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session) +61

[ADOException: could not initialize collection: [CustomerExample.BOL.Customer.Orders#2]]
NHibernate.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session) +154
NHibernate.Impl.SessionImpl.InitializeCollection(PersistentCollection collection, Boolean writing) +325
NHibernate.Collection.PersistentCollection.ForceInitialization() +201
NHibernate.Impl.SessionImpl.InitializeNonLazyCollections() +192
NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies) +137
NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object[] values, IType[] types, Object optionalObject, Object optionalID) +85
NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, Object optionalIdentifier) +117
NHibernate.Loader.EntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId) +63
NHibernate.Loader.EntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject) +45
NHibernate.Persister.EntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session) +276
NHibernate.Impl.SessionImpl.DoLoad(Type theClass, Object id, Object optionalObject, LockMode lockMode, Boolean checkDeleted) +720
NHibernate.Impl.SessionImpl.DoLoadByClass(Type clazz, Object id, Boolean checkDeleted, Boolean allowProxyCreation) +181
NHibernate.Impl.SessionImpl.Load(Type clazz, Object id) +128
CustomerExample.BLL.CustomerFacade.GetCustomer(Int32 customerId) in C:\My Visual Studio Projects\CustomerExample\CustomerExample\BLL\CustomerFacade.cs:34
CustomerExample._Default.BindOrders() in C:\My Visual Studio Projects\CustomerExample\CustomerExample\Default.aspx.cs:27
CustomerExample._Default.Page_Load(Object sender, EventArgs e) in C:\My Visual Studio Projects\CustomerExample\CustomerExample\Default.aspx.cs:21
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68
System.Web.UI.Control.OnLoad(EventArgs e) +88
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3034





The Order Mapping File

Quote:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="CustomerExample.BOL.Order, CustomerExample" table="Order">
<id name="Id" type="Int32" column="Id" >
<generator class="identity" />
</id>
<property name="CustomerId" column="CustomerId" type="Int32" />
<property name="Product" column="Product" type="String" length="50"/>
<property name="Price" column="Price" type="Double" />
</class>
</hibernate-mapping>



The Customer Mapping File

Quote:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="CustomerExample.BOL.Customer, CustomerExample" table="Customer">
<id name="Id" type="Int32" column="Id" >
<generator class="identity" />
</id>
<property name="Name" column="Name" type="String" length="50"/>
<set name="Orders">
<key column="CustomerId" />
<one-to-many
class="CustomerExample.BOL.Order, CustomerExample" />
</set>
</class>
</hibernate-mapping>


The errors occurs when the customer object is trying to load.

Code:

        public Customer GetCustomer(int customerId)
        {
            try
            {
                ISession session = ManagerSingleton.Instance.OpenSession();
                ITransaction transaction = session.BeginTransaction();

               Customer customer = (Customer)session.Load(typeof(Customer), customerId);

                transaction.Commit();
                session.Close();
                return customer;
            }
            catch(SqlException ex)
            {
                throw ex;
            }
        }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 12:29 pm 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
I had the same problem. I found that the Database didn't like the Object being called order as this is a keyword used in queries e.g ORDER BY. I changed the name of my class and table to CustomerOrder and it worked fine.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 12:33 pm 
Newbie

Joined: Tue Mar 21, 2006 3:52 pm
Posts: 5
Location: Atlanta, GA
THAT WORKED!!! THANKS A LOT. I can't believe I was wracking my brain over this all day yesterday.


Top
 Profile  
 
 Post subject: Another Solution
PostPosted: Wed Nov 29, 2006 5:58 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 5:33 pm
Posts: 28
Location: Chicago, IL
While I realize that you already have a solution to your problem, there is another aproach.

In short, you can put escape characters, back ticks, around the table name in your mapping file. Note that these are back ticks; the character under the ~ in the upper left of most keyboards.
Code:
<class name="CustomerExample.BOL.Order, CustomerExample" table="`Order`">



Sorry if this explanation is obvious but, when you reference an object in SQL Server that has a name that is the same as a keyword, you sometimes need to place square braces around the name in order to get the T-SQL to work.
Code:
SELECT * FROM [dbo].[ORDER]

Placing the back ticks around the table name in the mapping file indicates to NHibernate that the table name needs to have the square braces put around it in the generated SQL code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.