views:

287

answers:

3

I'm using a SqlDataSource to populate my GridView, because the two seem to be so tightly coupled together. Since this grid shows results of a search, I have a dynamic sql string being written in my codebehind that references parameters I pass in, such as below:

sdsResults.SelectParameters.Add("CodeID", TypeCode.String, strCodeID)

My problem is that the CodeID field is a varchar field. As you may have experienced, passing in an nvarchar field to be evaluated against a varchar field can be very detrimental to sql performance. However, SelectParameters.Add only takes in TypeCode types, which seems to only give me the unicode TypeCode.String as my viable option.

How do I force my SqlDataSource to use varchars? I can't change the datatype at this point--it's a main key of a large 10 year old app, and frankly, varchar is right for the application.

+1  A: 

BTW, my workaround right now lets SqlDataSource pass in the param as an nvarchar. My first line in my SQL then converts that nvarchar param explicitly to a varchar variable, and use that new varchar variable through my script instead.

But that seems silly.

A: 

Maybe this will work:

using System.Data;

sdsResults.SelectParameters.Add("CodeID", SqlDbType.VarChar, strCodeID);
Coentje
A: 

Nope, unfortunately it requires that a System.TypeCode param be passed in. Using a SqlDbType ends up in a compile error.