Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

Datatable "Type Expected" error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon - 29 Oct 2004 18:30 GMT
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
Jay B. Harlow [MVP - Outlook] - 29 Oct 2004 18:41 GMT
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

> I'm learning about datatables. When using the example provided by MS in
> the
[quoted text clipped - 52 lines]
>      Next i
>   End Sub
Jon - 29 Oct 2004 18:58 GMT
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

> Jon,
> > I get an error on line
[quoted text clipped - 76 lines]
> >      Next i
> >   End Sub
Jay B. Harlow [MVP - Outlook] - 30 Oct 2004 16:53 GMT
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

> Jay - this is the full program. I'm declaring myDataSet in
> the Declarations, but still getting the "type expected" error. Something
[quoted text clipped - 149 lines]
>   End Sub
> End Class

<<snip

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.