-1

Having problems selecting data from my database, any clue as to what is wrong as I've been staring at this for for a solid 2 hours.... (have made a login system to which can write and use data but can't search for it) enter image description here

enter image description here

4
  • 4
    Please post the C# including the entire SQL string. We can't even see the code behind the error in your image to help you here.
    – Jacob H
    Jun 7, 2017 at 12:29
  • Table is a reserved word and so needs escaping, if name of item is a column name it likewise needs escaping due to the spaces: select * from [TABLE] where [name of item] = 'xxx' - You also must use a parameterize statement as currently you have an SQL Injection vulnerability. You should also be using using () {} constructs to ensure disposal.
    – Alex K.
    Jun 7, 2017 at 12:29
  • You should follow best practices and don't name your table Table. Also use parameters for your values, see Best Practices - Executing Sql Statements.
    – Igor
    Jun 7, 2017 at 12:33
  • A simple rule of development is to not name anything that gives 'intellisensed' colors- it most likely is a reserved word
    – Mad Myche
    Jun 7, 2017 at 13:11

4 Answers 4

2

Few things to consider here:

  • DO NOT paste screenshots, copy/paste your code. It will be easier to read, easier to copy and to index for other SO users.
  • DO NOT concatenate strings as SQL commands, use parameters

You have syntax error, your WHERE shoud be like WHERE Item = '" + textbox.Text + "'";

Correct way to query database is to make parameterized query, like this:

SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
cmd.Connection = search;
cmd.CommandText = "SELECT * FROM [Table] WHERE Item = @item";
cmd.Parameters.Add("@item", SqlDbType.NVarChar).Value = txtLogin.Text;

sda.SelectCommand = cmd;

[Table] and Item should be real table name and column names from table you're trying to select data from. I'm guessing that in your case select should be something like SELECT * FROM [Users] where Username = ...

1
  • @Igor, thanks. I notices lack of single quotes but forgot Table issue. :)
    – Nino
    Jun 7, 2017 at 12:39
2

The problem is with SQL Query you are trying to execute.

Make sure if you are passing correct table name:

SELECT * FROM TABLE WHERE ...

Table is a keyword in SQL Server so it cannot be use like this in query.

Event if you have a table in db with name 'Table' then use following query

    SELECT * FROM [TABLE] WHERE ...
1

Table is a keyword on SQL you cant use it as is. If indeed you have named one of your tables as table, which I highly recommended not to, you must use brackets:

Select * from [Table]
0

The problem is with SQL Query you are trying to execute.

SELECT * FROM TABLE WHERE "condition";

use that query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.