0

I'm still somewhat new to ASP.NET and VB, and I found out that it's vastly different from the ASP I learned where I used Recordset to extract data from the database. Can someone give me some pointers on how to extract data from a database? Here is what I used to at least connect:

Dim conn As OdbcConnection
conn = New OdbcConnection("DSN=southwind")
Dim mystring as String = "SELECT GroupName FROM Group"
Dim cmd As OdbcCommand = New OdbcCommand(mystring, conn)

conn.Open()
Dim reader As OdbcDataReader = cmd.ExecuteReader()

The last line gives me an error saying:

Exception Details: System.Data.Odbc.OdbcException: ERROR [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near the keyword 'Group'.

But since I don't quite understand ASP.NET completely, not too sure what it means even though the syntax looks fine. Removing that line runs the code just fine. How would I display all the contents from the GroupName column in table Group?

EDIT: Thanks everyone, I completely forgot that Group was reserved in SQL.

5
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Mar 26, 2015 at 18:11
  • GROUP is a reserved word, part of the GROUP BY clause. That same query would not have worked with classic ASP either. Commented Mar 26, 2015 at 18:14
  • 1
    Looks like you're trying to connect to SQL Server. You might want to use the System.Data.Sql library instead of the ODBC stuff. Commented Mar 26, 2015 at 18:15
  • You really ought to change the name of the Group column if you can. You're colliding with a reserved word... Commented Mar 26, 2015 at 18:16
  • Is there an advantage to using System.Data.Sql over ODBC? And is there an example I can follow? Commented Mar 26, 2015 at 18:25

2 Answers 2

3

Group is a keyword in SQL, you need to wrap it in square brackets like this,

SELECT GroupName FROM [Group]

This would assume the Group to be a name of the table, instead of a key word; of GROUP BY clause.

Sign up to request clarification or add additional context in comments.

Comments

1

Group is a keyword in SQL. If your table name or column names referenced in your query are keywords, you can enclose them in brackets.

Dim mystring as String = "SELECT GroupName FROM [Group]"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.