JimRyanVB
Member
When I run this it successfully opens the datatable Color and returns a RecordCount = 179
However when I attempt to populate the lstColors it comes up empty?
=====================================================================================
However when I attempt to populate the lstColors it comes up empty?
VB.NET:
Public Class frmColors
Dim Colors As New dbControl
Private Sub CmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
Me.Close()
End
End Sub
Private Sub FrmColors_Shown(sender As Object, e As EventArgs) Handles MyBase.Load
GetColors()
End Sub
Private Sub GetColors()
Dim i As Integer
Colors.ExecQuery("SELECT ColorName FROM COLOR ORDER BY ColorName")
If NotEmpty(Colors.Exception) Then MsgBox(Colors.Exception) : Exit Sub
For i = 0 To Colors.RecordCount - 1
lstColors.Items.Add(Colors.DBDT)
Next
End Sub
Private Function NotEmpty(text As String) As Boolean
' CHECK IF ERRORS PRESENT
Return Not String.IsNullOrEmpty(text)
End Function
End Class
=====================================================================================
VB.NET:
Imports System.Data.OleDb
Public Class dbControl
' Create Your DB Connection
Private DBCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Prismwin\Store1\Store1.mdb;")
' Prepare DB Command
Private DbCmd As OleDbCommand
' DB Data
Public DBDA As OleDbDataAdapter
Public DBDT As DataTable
' Query Params
Public Params As New List(Of OleDbParameter)
' Query Stats
Public RecordCount As Integer
Public Exception As String
' Query SQL Command
Public Sub ExecQuery(Query As String)
'Reset Query Stats
RecordCount = 0
Exception = ""
Try
' Open a Connection
DBCon.Open()
' Create a New Command
DbCmd = New OleDbCommand(Query, DBCon)
' Load Params into DB Command
Params.ForEach(Sub(p) DbCmd.Parameters.Add(p))
' Clear Params List
Params.Clear()
' Execute Command & Fill DataTable
DBDT = New DataTable
DBDA = New OleDbDataAdapter(DbCmd)
RecordCount = DBDA.Fill(DBDT)
Catch ex As Exception
Exception = ex.Message
End Try
' Close Database
If DBCon.State = ConnectionState.Open Then DBCon.Close()
End Sub
' Include Query & Commands Params
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New OleDbParameter(Name, Value)
Params.Add(NewParam)
End Sub
End Class
Last edited by a moderator: