urgent help needed - datareader
Hi, can anyone tell me where I'm going wrong - I'm trying to populate a Listbox with the field "sedol" from my Access database. Here is the code:-
Public Class Form4
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(40, 24)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(192, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(40, 80)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(176, 173)
Me.ListBox1.TabIndex = 2
'
'Form4
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.TextBox1})
Me.Name = "Form4"
Me.Text = "Form4"
Me.ResumeLayout(False)
End Sub
#End Region
Public dbcConnection As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = n:\ca team\impart\mhdb.MDB;"
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
'refresh the list box based on the current search filter
Dim sSql As String
Dim cn As New OleDb.OleDbConnection(dbcConnection)
cn.Open()
'set sql string to reflect search criteria
sSql = "SEDOL, FROM Prices_table WHERE Sedol LIKE '%"
'open the ADO.NET Datareader
Dim cmd As New OleDb.OleDbCommand(sSql, cn)
Dim drd As OleDb.OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While drd.Read
ListBox1.Items.Add(drd!Sedol)
Loop
drd.Close()
End Sub
End Class
My Access Database is called mhdb.mdb and the table is called prices_table with fields Sedol, Name, Narrative, Bid price, Offer Price, Currency.
Thanks - I've been stuck on this for ages.