SQLDataReader to read query issue

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 13
Reputation: Atove is an unknown quantity at this point 
Solved Threads: 1
Atove's Avatar
Atove Atove is offline Offline
Newbie Poster

SQLDataReader to read query issue

 
0
  #1
Sep 30th, 2009
Hi all; I'd be really grateful if someone could assist me with this problem.

What i am trying to do is create a query that pulls information from two different tables: tblBooking and tblConcessions.
tblBooking holds the ID number of tblConcessions as a ForeignKey.
What i need within my statement is to pull some information from tblBooking and then replace the concessionID number with the concessionName from the concession table.
My program then reads this data and places it into a ListView box.

I've been able to get it to read just from one table; but i'm having trouble with pulling data from the other table.

My code so far is as follows.

  1. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
  2.  
  3. lvBookings.Items.Clear()
  4.  
  5. Dim itm As ListViewItem
  6.  
  7. Dim varCommand As String
  8.  
  9. varCommand = "SELECT BookingID, C_ID, StartDate, EndDate FROM tblBooking WHERE "
  10.  
  11. If Not txtBSBookingID.Text = Nothing Then
  12. varCommand = varCommand & "BookingID LIKE '" & txtBSBookingID.Text & "'"
  13. ElseIf cboInitials.SelectedIndex > -1 Or Not cboInitials.SelectedItem = "None" Then
  14. varCommand = varCommand & "FFInitiatedBy LIKE '" & cboInitials.SelectedItem & "'"
  15. End If
  16.  
  17. objCommand = New SqlCommand(varCommand, objConnection)
  18.  
  19. Try
  20. objConnection.Open()
  21. Dim reader As SqlDataReader = objCommand.ExecuteReader
  22.  
  23. While reader.Read()
  24. str(0) = reader("BookingID")
  25. str(1) = reader("C_ID")
  26. str(2) = reader("StartDate")
  27. str(3) = reader("EndDate")
  28. itm = New ListViewItem(str)
  29. lvBookings.Items.Add(itm)
  30.  
  31. End While
  32. Finally
  33. objConnection.Close()
  34. End Try

I have tried using a subquery; i have not used subqueries before so i'm sure i'm doing something wrong or missing some critical point.

This is what i tried:
  1. "SELECT BookingID, (SELECT ConcessionName " & _
  2. "FROM tblConcessions " & _
  3. "INNER JOIN tblBooking " & _
  4. "ON tblConcessions.ConcessionID=tblBooking.C_ID), "& _
  5. "StartDate, EndDate FROM tblBooking WHERE "

I got the error:
"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

Is a subquery the best way to go about achieving this or is there a better way?

Thanks for your time.
The world has been turned upside down to me...sounds like chaos...and i do love chaos!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 132
Reputation: babbu has a little shameless behaviour in the past 
Solved Threads: 13
babbu babbu is offline Offline
Junior Poster

Re: SQLDataReader to read query issue

 
1
  #2
Sep 30th, 2009
subquery is not the only way.
u can simply create another query and fetch the data from the other table.
alternatively u can also use data tables...
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 13
Reputation: Atove is an unknown quantity at this point 
Solved Threads: 1
Atove's Avatar
Atove Atove is offline Offline
Newbie Poster

Re: SQLDataReader to read query issue

 
0
  #3
Sep 30th, 2009
Awsome! Solved it! I've been stuck trying to figure this out for days.
I used 'INNER JOIN' to solve the problem:
  1. varCommand = "SELECT tblBooking.BookingID, tblConcessions.ConcessionName, " & _
  2. "tblGardenCentre.GardenCentreName, tblBooking.StartDate," & _
  3. "tblBooking.EndDate " & _
  4. "FROM tblBooking " & _
  5. "INNER JOIN tblConcessions " & _
  6. "ON tblBooking.C_ID = tblConcessions.ConcessionID " & _
  7. "INNER JOIN tblGardenCentre " & _
  8. "ON tblBooking.GC_ID = tblGardenCentre.GardenCentreID " & _
  9. "WHERE "
I just looked at the join statement in more detail and didn't use a subquery. I tried creating additional queries and then calling them and for some reason i couldn't get it to work in the i needed it to. Thanks for your time babbu!
The world has been turned upside down to me...sounds like chaos...and i do love chaos!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 294
Reputation: TomW is on a distinguished road 
Solved Threads: 38
TomW TomW is offline Offline
Posting Whiz in Training

Re: SQLDataReader to read query issue

 
1
  #4
Sep 30th, 2009
Dont forget to mark the thread as solved and rep babbu for helping ya.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC