View Single Post
  #3 (permalink)  
Old March 20th, 2006, 05:12 PM
Bob Bedell Bob Bedell is offline
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

If you wanted to assign a field value from an Access table to a base data type variable (like Doulbe) stored in a DataView, you might to something like the following in VB.NET (see third to last line of code for variable assignment). This is just a console app:

Code:
Sub Main()

        ' Open a connection to Access.
        Dim cnn As OleDbConnection
        Dim connectionString As String = _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=C:\JetBackendDB.mdb;" & _
            "User ID=Admin;" & _
            "Password="
        cnn = New OleDb.OleDbConnection(connectionString)
        cnn.Open()

        ' Create DataTable
        Dim ds As New DataSet
        With ds.Tables.Add("tblRecords")
            .Columns.Add("ID", GetType(Integer))
            .Columns.Add("DoubleField", GetType(Double))
        End With

        ' Load DataTable
        Dim cmd As New OleDbCommand
        cmd = New OleDbCommand("SELECT * FROM tblRecords", cnn)

        Dim rdr As OleDbDataReader = cmd.ExecuteReader()
        Dim row As DataRow
        Do While rdr.Read()
            row = ds.Tables("tblRecords").NewRow()
            row("ID") = rdr("ID")
            row("DoubleField") = rdr("DoubleField")
            ds.Tables("tblRecords").Rows.Add(row)
        Loop
        rdr.Close()

        ' Create DataView
        Dim vue As New DataView(ds.Tables("tblRecords"))

        ' Assign value in a DataView row to a Double variable.
        Dim vueRow As DataRowView = vue(0)
        Dim dblDoubleField As Double = vueRow("DoubleField")
        Console.WriteLine(dblDoubleField)
        Console.Read()

    End Sub
HTH,

Bob


Reply With Quote