Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Another Newby Question - Retreiving Data
Message
From
22/10/2004 02:37:45
 
 
To
21/10/2004 15:39:42
General information
Forum:
ASP.NET
Category:
Databases
Miscellaneous
Thread ID:
00951878
Message ID:
00953633
Views:
21
Gordon,

Try out this code.

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Form10
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 Button1 As System.Windows.Forms.Button
Friend WithEvents dgr As System.Windows.Forms.DataGrid
Friend WithEvents cbo1 As System.Windows.Forms.ComboBox
Private Sub InitializeComponent()
Me.dgr = New System.Windows.Forms.DataGrid
Me.Button1 = New System.Windows.Forms.Button
Me.cbo1 = New System.Windows.Forms.ComboBox
CType(Me.dgr, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'dgr
'
Me.dgr.DataMember = ""
Me.dgr.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgr.Location = New System.Drawing.Point(8, 8)
Me.dgr.Name = "dgr"
Me.dgr.Size = New System.Drawing.Size(504, 264)
Me.dgr.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(436, 280)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'cbo1
'
Me.cbo1.Location = New System.Drawing.Point(9, 280)
Me.cbo1.Name = "cbo1"
Me.cbo1.Size = New System.Drawing.Size(170, 23)
Me.cbo1.TabIndex = 2
Me.cbo1.Text = "ComboBox1"
'
'Form10
'
Me.AutoScaleBaseSize = New System.Drawing.Size(8, 14)
Me.ClientSize = New System.Drawing.Size(522, 319)
Me.Controls.Add(Me.cbo1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.dgr)
Me.Font = New System.Drawing.Font("Courier New", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.KeyPreview = True
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form10"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
Me.TopMost = True
CType(Me.dgr, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region
Dim cn As SqlClient.SqlConnection
Dim daOrders As SqlClient.SqlDataAdapter
Dim daOrderDetails As SqlClient.SqlDataAdapter
Dim ds As Data.DataSet
Dim dr As Data.DataRelation

Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn = New SqlClient.SqlConnection

cn.ConnectionString = _
"Server=(Local);" & _
"Database=Northwind;" & _
"Integrated Security=False;" & _
"Persist Security Info=False;" & _
"User ID=sa;" & _
"Password=DAI0524SY"

cn.Open()

' Create DataAdapters for each table
daOrders = New SqlDataAdapter("Select * from Orders", cn)
daOrderDetails = New SqlDataAdapter("Select * from [Order Details]", cn)

' Create a DataSet
ds = New DataSet

' Fill the DataSet with the data from each DataAdaptor. The DataAdaptors open and close the Connection
daOrders.Fill(ds, "Orders")
daOrderDetails.Fill(ds, "OrderDetails")

' Declare and create a relationship between the two tables
dr = ds.Relations.Add("R1", ds.Tables("Orders").Columns("OrderID"), _
ds.Tables("OrderDetails").Columns("OrderID"))

' Navigate through the tables
' Declare some variables to hold both parent and child records
Dim pRow, cRow As DataRow

' Declare a variable where the data will be dumped before displaying
Dim cOut As String
Dim x As Integer = 1

' Move through each row in the parent
For Each pRow In ds.Tables("Orders").Rows

' Load the value of the current record to the variable for display
cOut = pRow("OrderID").ToString() & Chr(10)

' Move through each row in the child
For Each cRow In pRow.GetChildRows(dr)

' Load the value of the current record to the variable for display
cOut = cOut & vbTab & cRow("ProductID").ToString() & ", " & cRow("UnitPrice").ToString() & Chr(10)

Next

' Display the records
MessageBox.Show(cOut)

If x = 5 Then Exit For
x += 1
Next

' Display the number of records
MessageBox.Show(ds.Tables("Orders").Rows.Count)

dgr.DataSource = ds.Tables("Orders")
cbo1.DataSource = ds.Tables("Orders")
cbo1.DisplayMember = "OrderID"

' Remove the relationship before removing the table
ds.Relations.Remove("R1")

' Remove the constraints as well
ds.Tables("OrderDetails").Constraints.Clear()

' Remove the table
ds.Tables.Remove("Orders")
ds.Tables.Remove("OrderDetails")

cn.Close()
End Sub
End Class
Shit happens!!!
Previous
Reply
Map
View

Click here to load this message in the networking platform