Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2004, 09:09 AM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default What's wrong with this SQL Statement

I am passing parameters to a function to select data from an SQL database.

I am looping through three arraylists to get data. My SQL statement:
        Dim strSQL As String = "SELECT Stand.StandID, Stand.Block, Purchase.MgtArea, " _
        & "Stand.TractNum, Purchase.TractName, BType.Btype, Strata.Stratum, " _
        & "Stand.Origin, Stand.Acres, Regime.SingleChop, Regime.SpotRake, Regime.Bed " _
        & "FROM (((Stand INNER JOIN Purchase ON Stand.TractNum = Purchase.PurchaseID) " _
        & "INNER JOIN BType ON Stand.BType = BType.Code) " _
        & "INNER JOIN Strata ON Stand.Stratum = Strata.Code) " _
        & "INNER JOIN Regime ON Stand.Regimeid = Regime.RegimeID " _
        & "WHERE Stand.TractNum = """ & selPurchase.Item(index) & """ AND BType.Btype = """ _
        & selBType.Item(bindex) & """ And Strata.Stratum = """ & selStratum.Item(sindex) & """"

This statement doesnt return any data though it doesnt produce an error message either.
I've checked with a query in the database and data is there. Also I've checked that data
is being passed.

The loop that is passing the data:
           For i = 0 To MMain.selPurchase.Count
                For b = 0 To MMain.selBType.Count
                    For s = 0 To selStratum.Count
                        objStands = getStands(i, b, s)
                        s = s + 1
                    Next s
                    b = b + 1
                Next b
                i = i + 1
            Next i

Also, is there a way to pass all of the array values at once and not loop?

Thanks,
dc
 
Old January 23rd, 2004, 10:17 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:& "WHERE Stand.TractNum = """ & selPurchase.Item(index) & """ AND BType.Btype = """ _
& selBType.Item(bindex) & """ And Strata.Stratum = """ & selStratum.Item(sindex) & """"
Your query has double quotes instead of single quotes. SQL Server takes single quotes for strings.
 
Old January 23rd, 2004, 10:49 AM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've tried several variations of single quotes but they turn the sql
statement into a comment.

& "WHERE Stand.TractNum = """ & selPurchase.Item(index) & """ AND BType.Btype = """ _
& selBType.Item(bindex) & """ And Strata.Stratum = """ & selStratum.Item(sindex) & """"

Which of the quotes need to be single?

Thanks
dc925

 
Old January 23rd, 2004, 11:05 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

& "WHERE Stand.TractNum = '" & selPurchase.Item(index) & "' AND BType.Btype = '" _
& selBType.Item(bindex) & "' And Strata.Stratum = '" & selStratum.Item(sindex) & "'"


The other thing you can do to is write the query with parameters. This simplifies the text of the SQL statement, and also provides some additional functionality with regards to the actual values used (automatic replacement of a single quote in a value to a pair of single quotes among other things).

& "WHERE Stand.TractNum = @tractnum AND BType.Btype = @btype And Strata.Stratum = @stratum"

sqlCommand.Parameters.Add("@tractnum", <sql datatype>).Value = selPurchase.Item(index)
sqlCommand.Parameters.Add("@btype", <sql datatype>).Value = selBType.Item(bindex)sqlCommand.Parameters.Add("@s tratum", <sql datatype>).Value = selStratum.Item(sindex)

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 23rd, 2004, 11:27 AM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. The quotes work fine. And I like the parameters.

dc:D






Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is wrong of my alert statement? Edward King Javascript How-To 1 May 28th, 2005 05:12 AM
What is wrong with this insert statement? method SQL Server 2000 13 April 27th, 2005 11:46 PM
What's wrong with this statement ? levinll SQL Language 4 April 29th, 2004 08:41 PM
What's wrong with my SQL statement? kaz VS.NET 2002/2003 1 December 11th, 2003 09:21 AM
what is wrong with this sql.. face Classic ASP Databases 5 September 23rd, 2003 03:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.