How do I force SqlDataSource to use varchar parameters?

I am using SqlDataSource to populate my GridView because the two seem to be so closely related to each other. Since this grid is showing search results, I have a dynamic sql string written in my code that references the parameters I pass, like below:

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

      

My problem is that the CodeID field is a varchar field. As you can see, jumping into an nvarchar field to evaluate against a varchar field can be very detrimental to sql performance. However, SelectParameters.Add only accepts TypeCode types, which seems to give me a unicode TypeCode.String as my viable option.

How do I make the SqlDataSource use varchars? I can't change the datatype at the moment - this is the master key of a large 10 year old application, and frankly varchar is fine for the application.

0


source to share


3 answers


By the way, my workaround right now is to pass the SqlDataSource in param as nvarchar. My first line in my SQL then converts that nvarchar explicitly to a varchar and uses the new varchar instead through my script.



But that seems silly.

+1


source


Maybe this will work:



using System.Data;

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

      

0


source


No, unfortunately this requires passing the System.TypeCode parameter. Using SqlDbType results in a compilation error.

0


source







All Articles