Question : The data types nvarchar and uniqueidentifier are incompatible in the add operator.

I am trying to use custom paging and sorting with an object datasource, so far so good until I try to filter it down by a guid, then I get the following error:

The data types nvarchar and uniqueidentifier are incompatible in the add operator.

Here's my SP code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ClientGetByAgentID]
(
      @sortExpression nvarchar(50),
      @startRowIndex INT,
      @maximumRows INT,
      @AgentID uniqueidentifier
)
AS
IF LEN(@sortExpression) = 0
SET @sortExpression = 'SpouseLastName'
-- Since @startRowIndex is zero-based in the data Web control, but one-based w/ROW_NUMBER(), increment
SET @startRowIndex = @startRowIndex + 1

DECLARE @sql nvarchar(4000)

SET @sql= 'SELECT ClientRowID, ClientID, AgentID, Title, FirstName, LastName, SouseTitle, SpouseFirstName, SpouseLastName, Address, Address2, City, State, Zip, Phone,
                         Cell, EmailAddress, Password, Notes
FROM        
(SELECT        ClientRowID, ClientID, AgentID, Title, FirstName, LastName, SouseTitle, SpouseFirstName, SpouseLastName, Address, Address2, City, State, Zip, Phone,
                         Cell, EmailAddress, Password, Notes,
ROW_NUMBER() OVER(ORDER BY ' + @sortExpression + ') as RowNum
FROM  Client WHERE AgentID = ' +  @AgentID + '
AS   Client
WHERE RowNum BETWEEN ' + CONVERT(nvarchar(10), @startRowIndex) +
                              ' AND (' + CONVERT(nvarchar(10), @startRowIndex) + ' + '
                              + CONVERT(nvarchar(10), @maximumRows) + ') - 1 AND  (AgentID = ' + @AgentID + ')'

-- Execute the SQL query
EXEC sp_executesql @sql

Answer : The data types nvarchar and uniqueidentifier are incompatible in the add operator.

Try it like this:

ALTER PROCEDURE [dbo].[ClientGetByAgentID]
(
      @sortExpression nvarchar(50),
      @startRowIndex INT,
      @maximumRows INT,
      @AgentID uniqueidentifier
)
AS
IF LEN(@sortExpression) = 0
SET @sortExpression = 'SpouseLastName'
-- Since @startRowIndex is zero-based in the data Web control, but one-based w/ROW_NUMBER(), increment
SET @startRowIndex = @startRowIndex + 1

DECLARE @sql nvarchar(4000)

SET @sql= 'SELECT ClientRowID, ClientID, AgentID, Title, FirstName, LastName, SouseTitle, SpouseFirstName, SpouseLastName, Address, Address2, City, State, Zip, Phone,
                         Cell, EmailAddress, Password, Notes
FROM        
(SELECT        ClientRowID, ClientID, AgentID, Title, FirstName, LastName, SouseTitle, SpouseFirstName, SpouseLastName, Address, Address2, City, State, Zip, Phone,
                         Cell, EmailAddress, Password, Notes,
ROW_NUMBER() OVER(ORDER BY ' + @sortExpression + ') as RowNum
FROM  Client WHERE AgentID = ''' +  cast(@AgentID as nvarchar(36))+ '''
AS   Client
WHERE RowNum BETWEEN ' + CONVERT(nvarchar(10), @startRowIndex) +
                              ' AND (' + CONVERT(nvarchar(10), @startRowIndex) + ' + '
                              + CONVERT(nvarchar(10), @maximumRows) + ') - 1 AND  (AgentID = ''' +  cast(@AgentID as nvarchar(36))+ ''')'

-- Execute the SQL query
EXEC sp_executesql @sql
Random Solutions  
 
programming4us programming4us