Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting SQL in string format

Status
Not open for further replies.

barrykellett

Programmer
Mar 4, 2003
29
GB
I am trying to use the following SQL code:

Code:
SELECT t.Title, DCount('[username]','[guestbook]','[threadid] = ' & gb.threadid) AS CountAllReplies, gb.username
FROM guestbook AS gb INNER JOIN thread AS t ON gb.threadid=t.threadid
WHERE (((gb.Original)=False))
ORDER BY gb.thedate DESC , t.Title, DCount("[username]","[guestbook]","[threadid] = " & gb.threadid), gb.username;

In my ASP.net page but when i code it as a string:

Code:
Const strOleDb as String = "SELECT t.Title, DCount(""[username]"",""[guestbook]"",""[threadid] = "" & gb.threadid) AS CountAllReplies, gb.username" & _
                "FROM guestbook AS gb INNER JOIN thread AS t ON gb.threadid=t.threadid" & _
                "WHERE (((gb.Original)=False))" & _
                "ORDER BY gb.thedate DESC , t.Title, DCount(""[username]"",""[guestbook]"",""[threadid]" = " & gb.threadid), gb.username;"

I get the error:
Conversion from 'Boolean' to 'String' cannot occur in a constant expression.
 
assuming the command you wrote in the first paragraph is correct, in ASP.NET you need:
Code:
Const strOleDb as String = "SELECT t.Title, DCount('[username]','[guestbook]','[threadid] = ' & gb.threadid) AS CountAllReplies, gb.username" & _
                "FROM guestbook AS gb INNER JOIN thread AS t ON gb.threadid=t.threadid" & _
                "WHERE (((gb.Original)=False))" & _
                "ORDER BY gb.thedate DESC , t.Title, DCount('[username]','[guestbook]','[threadid]' = & gb.threadid), gb.username;"

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Take a look at using ADO.NET parameters. Why?

1) It protects you from someone using a username of O'Brian

2) It protects you from SQL Injection attacks

3) Your system will perform faster because the database can cache your query

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top