Need some help with a SELECT statement

would like to list all rows in multi-line textbox

Page 1 of 1

3 Replies - 1021 Views - Last Post: 04 July 2009 - 10:22 PM Rate Topic: -----

#1 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Need some help with a SELECT statement

Post icon  Posted 04 July 2009 - 09:50 PM

I have a multiline textbox(txtAllRowsDisplayed) that I'd like to list all the selected rows from my query. This code list only one row, with columns 3,4,& 5 (which is what I want). But I'd like it to display all the rows that are selected into the textbox. thanks in advance :)
Maybe I need some sort of loop?

Try
Dim x As String = ""
Dim y As String = ""
Dim z As String = ""
cmd = New OleDbCommand("Select * from Candy where flavor ='" & txtFlavor.Text & "' AND company ='" & txtCompany.Text & "' AND quantity ='one'", cn)
dr = cmd.ExecuteReader
While dr.Read()
x = CStr(dr(3))
y = CStr(dr(4))
z = CStr(dr(5))
txtAllRowsDisplayed.Text = x + " " + y + " " + z
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try



Is This A Good Question/Topic? 0
  • +

Replies To: Need some help with a SELECT statement

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Need some help with a SELECT statement

Posted 04 July 2009 - 10:01 PM

Wow, ever heard of formatting your code?! Also, stop using legacy VB6 functions when programming in VB.NET (such as CStr and MsgBox), always use the native .Net libraries.

Now to your issue, it's only displaying the last row because you're overwriting what's in the TextBox on each iteration of your loop. Try this

Try
	Dim x As String = ""
	Dim y As String = ""
	Dim z As String = ""
	cmd = New OleDbCommand("Select * from Candy where flavor ='" & txtFlavor.Text & "' AND company ='" & txtCompany.Text & "' AND quantity ='one'", cn)
	dr = cmd.ExecuteReader
	While dr.Read()
			   'I got rid of your legacy functions and used
			   'native .Net libraries :)
		x = CType(dr(3), String)
		y = CType(dr(4), String)
		z = CType(dr(5), String)
		txtAllRowsDisplayed.Text &= x + " " + y + " " + z
	End While
Catch ex As Exception
	MessageBox.Show(ex.Message)
End Try


Was This Post Helpful? 0
  • +
  • -

#3 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Need some help with a SELECT statement

Posted 04 July 2009 - 10:17 PM

Quote

Wow, ever heard of formatting your code?! Also, stop using legacy VB6 functions when programming in VB.NET (such as CStr and MsgBox), always use the native .Net libraries.


Sorry :/ I'm self taught. So my mass of VB.Net knowledge comes from my one book and a sick lust for Google. As always i really appreciate your help Psycho :^:

I'll give this code a shot in the morning, thanks again!!!!
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Need some help with a SELECT statement

Posted 04 July 2009 - 10:22 PM

No problem, that's what we're here for :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1