Results 1 to 2 of 2

Thread: Load Data using DataReader in DataGrid

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Location
    India
    Posts
    1

    Question Load Data using DataReader 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
    Table Name: tblCustomerDetails
    Fields:CustomerId,CustomerSex,CustomerAddress,CustomerName
    =======================================
    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

  2. #2
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Exclamation

    Wow..... you are overwrite the "New" keyword.

    Try to use two difference tables for this purpose.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width