DStarkey
asked on
Dynamic Sql not populating asp.net datatable
I am new to using Dynamic Sqls and in my first attempt I have a stored procedure listed below that is dynamically building my sql string. When I execute the stored procedure with Sql Manager I get results back. But when i run it from my application the Datatable is not populated. Can someone take a look to make sure I am not missing anything within my procedure to return the results...
@StartDate Datetime,
@EndDate DateTime,
@RoleID int,
@tx nvarchar(50) = NULL,
@docid nvarchar(5)= NULL
As
Begin
BEGIN TRY
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
/* Variable Declaration */
Declare @SQLQuery AS NVarchar(4000)
Declare @ParamDefinition AS NVarchar(2000)
/* Build the Transact-SQL String with the input parameters */
Set @SQLQuery = 'Select * FROM CMSC.Orders o
INNER JOIN CMSC.WorkItems wi ON o.Orderid = wi.Orderid
INNER JOIN CMSC.WorkItemStatusDef wis on wis.StatusCode = wi.StatusCode
INNER JOIN CMSC.Inventory i on o.InvID = i.InvID
where (1=1) '
/* check for the condition and build the WHERE clause accordingly */
If (@StartDate Is Not Null) AND (@EndDate Is Not Null)
Set @SQLQuery = @SQLQuery + ' And (o.OrderDate
BETWEEN @StartDate AND @EndDate)'
If @RoleID Is Not Null
Set @SQLQuery = @SQLQuery + ' And (o.RoleID = @RoleID)'
If @tx Is Not Null
Set @SQLQuery = @SQLQuery + ' And (o.DocNum = @tx)'
If @docid Is Not Null
Set @SQLQuery = @SQLQuery + ' And (o.DocID = @docid)'
Set @ParamDefinition = '@StartDate DateTime,
@EndDate DateTime,
@RoleID int,
@tx nvarchar(50),
@docid nvarchar(5)'
/* Execute the Transact-SQL String with all parameter value's
Using sp_executesql Command */
Execute sp_Executesql @SQLQuery,
@ParamDefinition,
@StartDate,
@EndDate,
@RoleID,
@tx,
@docid
Run SQL profiler when you run your application to see what the application is actually sending to the SQL Database. See if that matches what you manually type in to get results. I suspect you have not defined a parameter correctly in your application if the SQL works fine.
Lee
Lee
ASKER
Thansk for the sugestion I never used the profiler before. Very handy...
Here is what the Profiler shows
exec CMSC.GetClientQuery @RoleID='76',@StartDate='1 /1/2010 12:00:00 AM',@EndDate='4/6/2010 12:00:00 AM',@tx='',@docid=''
I was able to extract the sql and found out that if I remove @tx and @docid from the line above, results come back. Also if I add NULL to the last two parameters I get the same resutls.
exec CMSC.GetClientQuery @RoleID='76',@StartDate='1 /1/2010 12:00:00 AM',@EndDate='4/6/2010 12:00:00 AM', @tx=NULL, @docid=NULL
How can I get the results to pass NULL instead of ' ' , Any ideas? Or do I need to look at doing something different within the stored procedure?
Thanks
Here is what the Profiler shows
exec CMSC.GetClientQuery @RoleID='76',@StartDate='1
I was able to extract the sql and found out that if I remove @tx and @docid from the line above, results come back. Also if I add NULL to the last two parameters I get the same resutls.
exec CMSC.GetClientQuery @RoleID='76',@StartDate='1
How can I get the results to pass NULL instead of ' ' , Any ideas? Or do I need to look at doing something different within the stored procedure?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help...
You're welcome.
Cheers,
Lee
Cheers,
Lee
ASKER
Thanks
Open in new window