Load Data using Reader in DataGrid
Hi all,
Could anyone clear my doubt. I use DataReader to get the datas & loaded the listview using it. But for loading the data to datagrid i use datatable filled using the same datareader. I get an error message as
" Column CustomerId does not belong to table dtCusDetails" when adding the fields to a datarow.
The code Follows.
lvoorders is listview name
=======================================
Public Class Form1
Inherits System.Windows.Forms.Form
Private Connection As OleDbConnection
Private Command As OleDbCommand
Private Reader As OleDbDataReader
Dim StrConn, StrQry As String
Dim drow As DataRow
Dim dtable As DataTable
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
dtable = New DataTable("dtCusDetails")
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\VBProj\Test\AdoOperations\Database\bank. mdb;"
StrQry = "Select * from tblCustomerDetails"
Command = New OleDbCommand()
With Command
.Connection = New OleDbConnection(StrConn)
.Connection.Open()
.CommandText() = StrQry
.CommandType = CommandType.Text
Reader = .ExecuteReader
End With
lvoorders.Items.Clear()
With Reader
While .Read
lvoorders.Items.Add(Reader.Item("CustomerName"))
drow = dtable.NewRow
drow.Item("CustomerId") = .Item("CustomerId")
drow.Item("CutomerName") = .Item("CustomerName")
drow.Item("CustomerSex") = .Item("CustomerSex")
drow.Item("CustomerAddress") = .Item("CustomerAddress")
dtable.Rows.Add(drow)
End While
End With
DataGrid1.DataSource = dtable
End Sub
=======================================
thanks in advance
Sen_Prog
:)
|