Bob, UK, 99946, red
Joe, UK, 87777, Blue
Mary, UK, 48565, green
The database name is TestDB, the table is InfoTable and the fields are Ename, Region, Enumber, Ecolour. Here’s the code…
Code:
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Public Class Test
Dim myString As String
Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub butQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butQuery.Click
myString = txtRegion.Text.ToString()
Dim cs As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\VB\Test\TestDB.mdb"
Dim cn As New OleDb.OleDbConnection(cs)
Dim dt As New DataTable
Dim dSet As New DataSet
Dim sqlString As String
sqlString = "SELECT Ename, Enumber, Ecolour FROM CMSDB WHERE Region = '" & myString & "'"
Dim cmd As New OleDb.OleDbCommand(sqlString, cn)
cmd.Parameters.Add("@myString", OleDb.OleDbType.VarChar, 30).Value = myString
Dim da As New OleDb.OleDbDataAdapter(cmd)
da.Fill(dt)
End Sub
End Class
Code:
For Each dRow As DataRow In dSet.Tables("InfoTable").Rows
For Each dCol As DataColumn In dRow
txtQuery.Text = txtQuery.Text & " " & dCol.ToString()
Next dCol
txtQuery.Text = txtQuery.Text & vbCrLf
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…
Code:
For Each dCol As DataColumn In dRow
Cheers,
Jack

