Error Input string was not in a correct format

J

Jose Oliver

I am a bit baffled on this error. A windows app written in C# is
attempting to execute the following query:

SELECT *
FROM Employee
WHERE (Name LIKE '%' + @PARAM1 + '%') AND (Name LIKE '%' + @PARAM2
+ '%') AND (Company LIKE '%' + @PARAM3 + '%')

I am getting the following error - "Input string was not in a correct
format."
 
G

Guest

Jose,
I'm not sure this is really a C# language group question, but--
The SQL statement you show would be fine if it was inside a stored procedure
and the @PARAMxx parameter variables were sproc input parameters.

However if this is, instead, an inline string concatenation in a block of C#
code, it is very wrong. You should be using a parameterized query in which
case it would look more like the following:

string strSQL=@"SELECT * FROM Employee WHERE (Name LIKE '%' + @PARAM1 + '%')
AND (Name LIKE '%' + @PARAM2 + '%') AND (Company LIKE '%' + @PARAM3 + '%')"

then,
cmd.Parameters.AddWithValue("@PARAM1", txtBoxParam1.Text ); // etc.

Hope that helps.

-- Peter
http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Please clarify if this is ni C# or the actual query being executed in SQL

If you are concatenating the values in C# maybe you have a ' character in
one of the parameters.

In any case post the piece of code (C#) where you are getting the error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top