By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,101 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,101 IT Pros & Developers. It's quick & easy.

Help with display query to a multiline textbox

P: 6
Hi, I’m having trouble with my code and I’m starting to get lost. The Program takes the country that’s been typed in to the textbox and when the query button is clicked the program queries an access database for all records that contain the selected country. I think that part works, but I’m having trouble with the output. I want to display only certain fields of the record in a multiline text box, each matching record to a new line, like this…
Bob, UK, 99946, red
Joe, UK, 87777, Blue
Mary, UK, 48565, green

The database I name is TestDB, the table is InfoTable and the fields are Ename, Region, Enumber, Ecolour. Here’s the code…

Expand|Select|Wrap|Line Numbers
  1.  Imports System.IO
  2. Imports System.Data
  3. Imports System.Data.OleDb
  4. Public Class Test
  5.     Dim myString As String
  7.     Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.     End Sub
  12.     Private Sub butQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butQuery.Click
  13.         myString = txtRegion.Text.ToString()
  14.         Dim cs As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\VB\Test\TestDB.mdb"
  15.         Dim cn As New OleDb.OleDbConnection(cs)
  16.         Dim dt As New DataTable
  17.         Dim dSet As New DataSet
  18.         Dim sqlString As String
  20.         sqlString = "SELECT Ename, Enumber, Ecolour FROM CMSDB WHERE Region = '" & myString & "'"
  21.         Dim cmd As New OleDb.OleDbCommand(sqlString, cn)
  22.         cmd.Parameters.Add("@myString", OleDb.OleDbType.VarChar, 30).Value = myString
  23.         Dim da As New OleDb.OleDbDataAdapter(cmd)
  24. da.Fill(dt)
  26.     End Sub
  28. End Class
Now thats the code and as far as I know it works ok, but when I try the code for the output to the multiline textbox it doesn’t work so good. Here’s the code… (this code goes right under the da.Fill(dt) above…

Expand|Select|Wrap|Line Numbers
  3.         For Each dRow As DataRow In dSet.Tables("myTable").Rows
  4.             For Each dCol As DataColumn In dRow
  5.                 txtQuery.Text = txtQuery.Text & " " & dCol.ToString()
  6.             Next dCol
  8.             txtQuery.Text = txtQuery.Text & vbCrLf
  9.         Next dRow 

If anyone can help me get the code working that would be great.
I also seem to get this error “Expression is of type 'System.Data.DataRow', which is not a collection type.” The error pops up on dRow from this line…
Expand|Select|Wrap|Line Numbers
  1.  For Each dCol As DataColumn In dRow 
Could someone help me get the output code working?
Mar 21 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.