Simon Ward
Simon Ward

Reputation: 73

Issue With Translating SQL Item to String

I have searched the internet for an answer to my current question, but have found nothing. I have an SQL database that I am trying to get an item from as a string. My database is as follows: Database Table Table Data

Now, I try to get a 95Number in the following code:

connection.Open();
readerUsers = commandUsers.ExecuteReader();
while (readerUsers.Read())
           ...
                IDNumber = readerUsers[2].ToString();
                MessageBox.Show(IDNumber);

Unfortunately, every MessageBox.Show(IDNumber); that I call only returns "95". I have also tried IDNumber = readerUsers["95Number"].ToString();, as was recommended online, and that only brought about a fatal error. What am I doing wrong here? Thanks for your time, and have a great day!

EDIT: The query for commandUsers is as follows:

stringUsers = "SELECT IDuser, Username, 95Number, Password, Active, Admin, Teacher FROM dbo.Users;";
commandUsers = new SqlCommand(stringUsers, connection);

Upvotes: 0

Views: 47

Answers (1)

Circle Hsiao
Circle Hsiao

Reputation: 1977

In SELECT 95Number the 95 will be recognized as a value instead of column name. It's not considered a good practice that using number at the beginning of column name, perhaps you can change the name to Number95?

If you really want to keep the column name as the way it is. Put the column name in square brackets will make the query works as your expectation.

SELECT [95Number] FROM Users

Upvotes: 1

Related Questions