Thomas
Thomas

Reputation: 34198

Issue with SqlParameter and data type

I am just calling a stored procedure which was developed with SQL Server 2005. Here is the signature of my stored proc.

CREATE PROCEDURE [dbo].[GenericSearch]
(
@ModuleName VARCHAR(100),
@ViewName VARCHAR(100),
@WhereClause_XML XML,
@LogicalOperator VARCHAR(3)
)
AS
BEGIN

END

I just call this stored procedure from my front end and pass value like

string[] Tables = new string[] { "TPMaster" };
SqlParameter[] cparams = new SqlParameter[4];
SqlParameter param;
param = new SqlParameter("@ModuleName", ModuleName);
param.DbType = DbType.String;
cparams[0] = param;

param = new SqlParameter("@ViewName", DataSource);
param.DbType = DbType.String;
cparams[1] = param;

param = new SqlParameter("@WhereClause_XML", strXml);
param.DbType = DbType.Xml;
cparams[2] = param;

param = new SqlParameter("@LogicalOperator", (rbAnd.Checked ? "AND" : "OR"));
param.DbType = DbType.String;
cparams[3] = param;

ds = Business.Common.GetDataSet("GenericSearch", cparams, Tables);

Here I specify that param.DbType string for all my varchar datatype for stored proc and one xml data type.

I am getting an error

Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'

I just do not understand what causes the error. Please tell me how to fix it.

Upvotes: 1

Views: 5162

Answers (3)

Jeff Reddy
Jeff Reddy

Reputation: 5670

Your using sp_executesql, and not passing the correct type of argument. @statement is the required argument for sp_executesql and has to be a unicode constant or unicode variable. I'm assuming your building a sql statement based upon the values your accepting into your stored proc.

Consider this example:

DECLARE @FieldName VARCHAR(100),@TableName VARCHAR(100),@WhereClause varchar(100)

SET @FieldName = 'ItemID'
SET @TableName = 'Item'
SET @WhereClause = 'ItemID > 200' 

DECLARE @yourSql varchar(max)

SET @yourSql = 'SELECT ' + @FieldName + ' FROM ' + @TableName + ' WHERE ' + @WhereClause

EXEC sp_executesql @yourSql

This should throw the same exception your getting. However, change the declaration of the variable @yourSql from a varchar to an nvarchar like this:

DECLARE @yourSql nvarchar(max)

And you're in the clear.

Upvotes: 0

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112632

Use DbType.AnsiString for VARCHAR and DbType.String for NVARCHAR.

Here is a code snippet that I used in a O/R-mapper:

switch (systemType) {
    case 127: // bigint
        return DbType.Int64;
    case 173: // binary
    case 189: // timestamp
    case 165: // varbinary, varbinary MAX
    case 34: // image
        return DbType.Binary;
    case 98: // sql_variant
        return DbType.Object;
    case 104: // bit
        return DbType.Boolean;
    case 175: // char
        return DbType.AnsiStringFixedLength;
    case 61: // datetime
    case 58: // smalldatetime
        return DbType.DateTime;
    case 106: // decimal
    case 108: // numeric
        return DbType.Decimal;
    case 62: // float
        return DbType.Double;
    case 56: // int
        return DbType.Int32;
    case 60: // money
    case 122: // smallmoney
        return DbType.Currency;
    case 239: // nchar
        return DbType.StringFixedLength;
    case 99: // ntext
    case 231: // nvarchar, nvarchar MAX
        return DbType.String;
    case 59: // real
        return DbType.Single;
    case 52: // smallint
        return DbType.Int16;
    case 35: // text
    case 167: // varchar, varchar MAX
        return DbType.AnsiString;
    case 48: // tinyint
        return DbType.Byte;
    case 36: // uniqueidentifier
        return DbType.Guid;
    case 241: // xml
        return DbType.Xml;
    default:
        return (DbType)(-1);
}

Upvotes: 0

Paulo Santos
Paulo Santos

Reputation: 11587

Check the body of the stored procedure, and the internal calls it makes.

Probably you're making a call to sp_executesql and are passing a simple varchar as the body of the SQL command.

Upvotes: 1

Related Questions