JohnAtrik
JohnAtrik

Reputation: 175

LINQ to SQL DataContext "String must be exactly one character long."

I'm using a Stored Procedure in a Web Service connection by LINQ to SQL. When I manually execute my stored procedure I see it return the columns I need, but when I try on my Web Service using LINQ to SQL I get "String must be exactly one character long." error thrown.

Here are more details:

Stored Procedure expects four parameters: char(12), varchar(3), int, varchar(3) output

When I do: exec spName '123456789012', '123', '1', ''

I get the correct columns returned to me (in SQL Server 2012), however when I call this from my Web Service by the following code:

string str1 = "123456789012";
string str2 = "123";
int? int1 = 1;
string str3 = String.Empty;

spNameResult result = context.spName(str1, str2, int1, ref str3).FirstOrDefault<spNameResult>();

It throws the error of needing the string to be exactly one character long.

So, I get to my question: Why would the exec work, but the Web Service call with the same parameters does not and how can I resolve that?

Upvotes: 1

Views: 878

Answers (1)

huhu78
huhu78

Reputation: 389

In your example, third parameter is given in apostrophes - like it was varchar (and not int). In c# example there is int type for third parameter. Please review your stored procedure definition and c# generated classes. Maybe there is type mess on third parameter. EXEC works because SQL Server can cast parameters between INT and VARCHAR types.

Also use SQL Profiler to catch SQL query. I guess LINQ builds SQL query with params and not direct arguments.

Upvotes: 1

Related Questions