Datatable "Type Expected" error

G

Guest

I'm learning about datatables. When using the example provided by MS in the
..NET Framework Class Library for DATATABLE (see below) I get an error on line
3 that says "Type expected". Is something missing from the code?

Thanks - Jon

Private Sub MakeParentTable()
' Create a new DataTable.
Dim myDataTable As Datatable = New Datatable("ParentTable")

' Declare variables for DataColumn and DataRow objects.
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow

' Create new DataColumn, set DataType, ColumnName and add to
DataTable.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.Int32")
myDataColumn.ColumnName = "id"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn)

' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "ParentItem"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ParentItem"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
' Add the column to the table.
myDataTable.Columns.Add(myDataColumn)

' Make the ID column the primary key column.
Dim PrimaryKeyColumns(0) As DataColumn
PrimaryKeyColumns(0) = myDataTable.Columns("id")
myDataTable.PrimaryKey = PrimaryKeyColumns

' Instantiate the DataSet variable.
myDataSet = New DataSet
' Add the new DataTable to the DataSet.
myDataSet.Tables.Add(myDataTable)

' Create three new DataRow objects and add them to the DataTable
Dim i As Integer
For i = 0 To 2
myDataRow = myDataTable.NewRow()
myDataRow("id") = i
myDataRow("ParentItem") = "ParentItem " + i.ToString()
myDataTable.Rows.Add(myDataRow)
Next i
End Sub
 
J

Jay B. Harlow [MVP - Outlook]

Jon,
I get an error on line
3 that says "Type expected". Is something missing from the code?

What specifically does Line 3 look like? The function you gave works as
expected when you declare the 'myDataSet' variable.


FWIW:

Rather then using Type.GetType with a string, I would recommend using
GetType with the type's identifier:

Instead of:
myDataColumn.DataType = System.Type.GetType("System.Int32")
Use:
myDataColumn.DataType = GetType(System.Int32)

As it avoids possible hard to find run time errors in favor of easy to spot
compile time errors.

Hope this helps
Jay
 
G

Guest

Jay - this is the full program. I'm declaring myDataSet in
the Declarations, but still getting the "type expected" error. Something must
still be out of place...

thanks - Jon

------------------------------------------------------

Imports System.Data

Public Class Form1
Inherits System.Windows.Forms.Form

' Put the next line into the Declarations section.
Private myDataSet As DataSet

#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 DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(64, 40)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(584, 288)
Me.DataGrid1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
Me.ClientSize = New System.Drawing.Size(736, 417)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region


Private Sub MakeDataTables()
' Run all of the functions.
MakeParentTable()
MakeDataRelation()
BindToDataGrid()
End Sub

Private Sub MakeParentTable()
' Create a new DataTable.

Dim myDataTable As Datatable = New Datatable("ParentTable")

' Declare variables for DataColumn and DataRow objects.
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow

' Create new DataColumn, set DataType, ColumnName and add to
DataTable.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.Int32")
myDataColumn.ColumnName = "id"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn)

' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "ParentItem"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ParentItem"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
' Add the column to the table.
myDataTable.Columns.Add(myDataColumn)

' Make the ID column the primary key column.
Dim PrimaryKeyColumns(0) As DataColumn
PrimaryKeyColumns(0) = myDataTable.Columns("id")
myDataTable.PrimaryKey = PrimaryKeyColumns

' Instantiate the DataSet variable.
myDataSet = New DataSet
' Add the new DataTable to the DataSet.
myDataSet.Tables.Add(myDataTable)

' Create three new DataRow objects and add them to the DataTable
Dim i As Integer
For i = 0 To 2
myDataRow = myDataTable.NewRow()
myDataRow("id") = i
myDataRow("ParentItem") = "ParentItem " + i.ToString()
myDataTable.Rows.Add(myDataRow)
Next i
End Sub


Private Sub MakeDataRelation()
' DataRelation requires two DataColumn (parent and child) and a name.
Dim myDataRelation As DataRelation
Dim parentColumn As DataColumn
Dim childColumn As DataColumn
parentColumn = myDataSet.Tables("ParentTable").Columns("id")
childColumn = myDataSet.Tables("ChildTable").Columns("ParentID")
myDataRelation = New DataRelation("parent2Child", parentColumn,
childColumn)
myDataSet.Tables("ChildTable").ParentRelations.Add(myDataRelation)
End Sub

Private Sub BindToDataGrid()
' Instruct the DataGrid to bind to the DataSet, with the
' ParentTable as the topmost DataTable.
DataGrid1.SetDataBinding(myDataSet, "ParentTable")
End Sub
End Class
 
J

Jay B. Harlow [MVP - Outlook]

Jon,
The code you posted compiles fine under VS.NET 2003.

The routines you included aren't called, so obviously the form runs fine
also.

If I call MakeDataTables, I get an "Object reference not set to an instance
of an object" when retrieving the childColumn in MakeDataRelation routine,
as you do not include code to that actually defines the ChildTable.

If you want to post the actual form that is failing, or email me that would
be great, otherwise I don't see how we can help you any more then we are.

Thanks for understanding.

Hope this helps
Jay

Jon said:
Jay - this is the full program. I'm declaring myDataSet in
the Declarations, but still getting the "type expected" error. Something
must
still be out of place...

thanks - Jon

------------------------------------------------------

Imports System.Data

Public Class Form1
Inherits System.Windows.Forms.Form

' Put the next line into the Declarations section.
Private myDataSet As DataSet

#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 DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor =
System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(64, 40)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(584, 288)
Me.DataGrid1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
Me.ClientSize = New System.Drawing.Size(736, 417)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region


Private Sub MakeDataTables()
' Run all of the functions.
MakeParentTable()
MakeDataRelation()
BindToDataGrid()
End Sub

Private Sub MakeParentTable()
' Create a new DataTable.

Dim myDataTable As Datatable = New Datatable("ParentTable")

' Declare variables for DataColumn and DataRow objects.
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow

' Create new DataColumn, set DataType, ColumnName and add to
DataTable.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.Int32")
myDataColumn.ColumnName = "id"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn)

' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "ParentItem"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ParentItem"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
' Add the column to the table.
myDataTable.Columns.Add(myDataColumn)

' Make the ID column the primary key column.
Dim PrimaryKeyColumns(0) As DataColumn
PrimaryKeyColumns(0) = myDataTable.Columns("id")
myDataTable.PrimaryKey = PrimaryKeyColumns

' Instantiate the DataSet variable.
myDataSet = New DataSet
' Add the new DataTable to the DataSet.
myDataSet.Tables.Add(myDataTable)

' Create three new DataRow objects and add them to the DataTable
Dim i As Integer
For i = 0 To 2
myDataRow = myDataTable.NewRow()
myDataRow("id") = i
myDataRow("ParentItem") = "ParentItem " + i.ToString()
myDataTable.Rows.Add(myDataRow)
Next i
End Sub


Private Sub MakeDataRelation()
' DataRelation requires two DataColumn (parent and child) and a name.
Dim myDataRelation As DataRelation
Dim parentColumn As DataColumn
Dim childColumn As DataColumn
parentColumn = myDataSet.Tables("ParentTable").Columns("id")
childColumn = myDataSet.Tables("ChildTable").Columns("ParentID")
myDataRelation = New DataRelation("parent2Child", parentColumn,
childColumn)
myDataSet.Tables("ChildTable").ParentRelations.Add(myDataRelation)
End Sub

Private Sub BindToDataGrid()
' Instruct the DataGrid to bind to the DataSet, with the
' ParentTable as the topmost DataTable.
DataGrid1.SetDataBinding(myDataSet, "ParentTable")
End Sub
End Class
<<snip>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top