Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
i just faced a problem with my sql statement in my vb.net project.can you check my statement?
VB
cmd = New OleDbCommand("SELECT [sub_pay_item_quantity.quantity],[sub_pay_item_unit_rate.rate] FROM sub_pay_item_quantity,sub_pay_item_unit_rate WHERE sub_pay_item_quantity.sub item=sub_pay_item_unit_rate.sub item and                        
[sub_pay_item_quantity.sub item]='" & subItem & "' and [sub_pay_item_quantity.bridge type]='" & bridgeType & "' and [sub_pay_item_quantity.span]='" & span & "'", conn)
Posted
Comments
Richard C Bishop 30-Oct-13 10:55am    
You cannot do a Select statement like that. If you want to query multiple tables, you must join them in some fashion.
Kschuler 30-Oct-13 17:13pm    
It's possible to do this with some DBs...just not good practice. If you separate the file names with a comma in the FROM I believe it assumes an inner join. You'll need to put the conditions that would have gone in the "ON" in the WHERE clause instead. It's a much better practice to specifically state the JOIN. The code will be much easier to understand and easier to maintain and/or change or later.
Richard C Bishop 30-Oct-13 17:16pm    
Very well, thank you for that information.

1 solution

VB
cmd = New OleDbCommand("SELECT [sub_pay_item_quantity].[quantity], [sub_pay_item_unit_rate].[rate] FROM [sub_pay_item_quantity], [sub_pay_item_unit_rate] WHERE [sub_pay_item_quantity].[sub item]=[sub_pay_item_unit_rate].[sub item] AND [sub_pay_item_quantity].[sub item]='" & subItem & "' AND [sub_pay_item_quantity].[bridge type]='" & bridgeType & "' AND [sub_pay_item_quantity].[span]='" & span & "'", conn)


The problem obviously is in the way you encapsulate table and column names between brackets ; as you can see, the right syntax is [table].[column], not [table.column] (at least with SQL Server, but you did not tag which database system you are using).

Note: constructing a SQL statement by concatenating text fields like you do is a very bad habit, because this leaves your code opened for SQL injection attacks. You should use parameterized queries instead (see corrected example prepared for parameterized query):
VB
cmd = New OleDbCommand("SELECT [sub_pay_item_quantity].[quantity], [sub_pay_item_unit_rate].[rate] FROM [sub_pay_item_quantity], [sub_pay_item_unit_rate] WHERE [sub_pay_item_quantity].[sub item]=[sub_pay_item_unit_rate].[sub item] AND [sub_pay_item_quantity].[sub item]=@subItem AND [sub_pay_item_quantity].[bridge type]=@bridgeType AND [sub_pay_item_quantity].[span]=@span", conn)
 
Share this answer
 
Comments
Nebilo 31-Oct-13 3:25am    
Thank you for the code,but a new exception arose.It says index out of range exception was unhandled.
Here's the code.

data_reader = cmd.ExecuteReader()
If data_reader.HasRows = True Then
Do While data_reader.Read()
payItem = CDbl(data_reader.Item("pay item"))
subpayItem = CDbl(data_reader.Item("sub pay item"))
subItem = data_reader.Item("sub item")
unit = data_reader.Item("unit")
bridgeType = data_reader.Item("bridge type")
span = (data_reader.Item("span").ToString())
quantity = CDbl(data_reader.Item("quantity"))
rate = CDbl(data_reader.Item("rate"))
phil.o 31-Oct-13 3:47am    
On which line is the exception thrown?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900