Click here to Skip to main content
15,881,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an SQL database, it contains a table named (users), the name field is defined as text.
In the C# code I want to search the database for a specific name like this:
C#
SQLCommand c = new SQLCommand(connection);
c.CommandType = CommandType.Text;
c.CommandText = "SELECT * FROM users WHERE name=\'" + textbox1.text + "\'";
adapter.Fill(dataset1,"users");

When I execute it returns this error:

The data types text and varchar are incompatible in the equal to operator.

Any ideas?
Posted
Updated 26-Nov-13 7:36am
v2
Comments
Herman<T>.Instance 26-Nov-13 13:47pm    
Why have you choosen for TEXT as columntype in stead of NVARCHAR?

I would suggest you to use Stored procedure instead command text in a code behind.

Please, see: Return Data from a Stored Procedure[^]
How to: Execute a Stored Procedure that Returns Rows[^]

SP should looks like this one:
SQL
SELECT *
FROM [users]
WHERE [name]=@username


By The Way: Do not use reserved words[^] as a name of fields, etc.
 
Share this answer
 
v2
Comments
RaisKazi 26-Nov-13 15:46pm    
My 5!
Maciej Los 26-Nov-13 15:47pm    
Thank you ;)
c.CommandText = "SELECT * FROM users WHERE name Like '" + textbox1.text + "'";
or
c.CommandText = "SELECT * FROM users WHERE CONVERT(VARCHAR, name) ='" + textbox1.text + "'";

text is deprecated Changing to varchar(max) will be easier to work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900