-->
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.  [ 2 posts ] 
Author Message
 Post subject: Urgent Help: Passing NULL value to stored procedure.
PostPosted: Fri Apr 02, 2010 4:16 pm 
Newbie

Joined: Fri Apr 02, 2010 3:43 pm
Posts: 3
Hi All,

I have a stored procedure which accepts about 2 parameters, I am including a sample for the same below.

=================================================================
Code:
CREATE PROCEDURE App_Products_GetList

         @Id      NUMERIC(18,0) = NULL,
         @Name   VARCHAR(50) = NULL

AS BEGIN

      SELECT
            [Id]
            ,[Name]
      FROM
            [Products]
      WHERE
            (([Id] = @Id AND @Id IS NOT NULL) OR @Id IS NULL) AND
            (([Name] LIKE @Name AND @Name IS NOT NULL) OR @Name IS NULL)
END


Now, I want to pass null values to both @Id and @Name in certain cases. For this I tried the following.

Code:
            IQuery iQuery = NHibernateHelper.GetCurrentSession().GetNamedQuery("App_Products_GetList_1");
            iQuery.SetString("Id", "NULL");
            iQuery.SetString("Name", "NULL");
            IList<Product> ilist_1  = iQuery.List<Product>() as List<Product>;


=================================================================
At this point I am getting an error. [could not execute query], with the underlying details


Error converting data type nvarchar to numeric.
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: Error converting data type nvarchar to numeric.

Source Error:

IList<Product> ilist_1 = iQuery.List<Product>() as List<Product>;


=================================================================

I checked the profiler code which outputs the content as,
Code:
exec sp_executesql N'exec [App_Products_GetList] @p0,@p1', N'@p0 nvarchar(4),@p1 nvarchar(12)',  @p0=N'NULL',@p1=N'NULL'


Here too the error message which I have encountered is: Error converting data type nvarchar to numeric.
=================================================================

I would like to know, how to pass NULL values to the stored procedure through the above mentioned call.

Regards,
Sougandh Pavithran


Top
 Profile  
 
 Post subject: Re: Urgent Help: Passing NULL value to stored procedure.
PostPosted: Sat Apr 03, 2010 12:26 am 
Newbie

Joined: Fri Apr 02, 2010 3:43 pm
Posts: 3
Please note the issue has been solved. I found a solution to the problem in the documentation(NHibernationinAction.pdf). I have also included the modifified code below for future reference, just if incase someone again commits the same blunder.

Code:
            IQuery iQuery = NHibernateHelper.GetCurrentSession().GetNamedQuery("App_Products_GetList_1");
            iQuery.SetString("Id", null);
            iQuery.SetString("Name", null);
            IList<Product> ilist_1  = iQuery.List<Product>() as List<Product>;



Regards,
Sougandh Pavithran.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.