"Specified cast is not valid" when MyTable.Select is used

A

Al Repasky

I get this error with the code below. I think it is the line that
strExp is declared. I have tried the ID and not the tYear column and
still get the error.

All I want to do is sort the years.

Thanks,
ripripal




Private Function GetYears()

Dim myRow, dr As DataRow
Dim str As String
Dim i, ib, ie, icnt As Integer
Dim myDataTable As New DataTable

'Declare DataColumn ID
Dim myColumn1 As New DataColumn("ID")
myColumn1.DataType = System.Type.GetType("System.Int32")
'Add Column to table
myDataTable.Columns.Add(myColumn1)


'Declare DataColumn tYear
Dim myColumn2 As New DataColumn("tYear")
myColumn2.DataType = System.Type.GetType("System.Int32")
'Add Column to table
myDataTable.Columns.Add(myColumn2)
'Get rid of duplicates
Dim myUC As New UniqueConstraint("UC",
myDataTable.Columns("tYear"))
myDataTable.Constraints.Add(myUC)

icnt = 0
For Each dr In dsAutos.Tables("tAutos").Rows
icnt = icnt + 1
ib = Val(dr("tBYr"))
i = ib
ie = Val(dr("tEYr"))
Do Until i = ie + 1
Try
myRow = myDataTable.NewRow()
myRow("ID") = icnt
myRow("tYear") = i
Console.WriteLine(myRow("tYear"))
myDataTable.Rows.Add(myRow)
'Me.ListBoxYear.Items.Add(CStr(i))
i = i + 1
Catch
i = i + 1
End Try
Loop
Next
Console.WriteLine(icnt)
Console.WriteLine("-----")

For Each dr In myDataTable.Rows
Console.WriteLine(dr("tYear"))
Next

Dim strExp, strSort As String

'Sort
strExp = "tYear > 1900"
strSort = "tYear"

'Use the Select method to find all rows matching the filter.
Dim drsort As DataRow() = myDataTable.Select(strExp, strSort,
DataViewRowState.Added)

For Each drsort In myDataTable.Rows
Console.WriteLine(drsort("tYear"))
Next

End Function
 
M

Miha Markic [MVP C#]

Al,

Below

Al Repasky said:
I get this error with the code below. I think it is the line that
strExp is declared. I have tried the ID and not the tYear column and
still get the error.

All I want to do is sort the years.

Thanks,
ripripal




Private Function GetYears()

Dim myRow, dr As DataRow
Dim str As String
Dim i, ib, ie, icnt As Integer
Dim myDataTable As New DataTable

'Declare DataColumn ID
Dim myColumn1 As New DataColumn("ID")
myColumn1.DataType = System.Type.GetType("System.Int32")
'Add Column to table
myDataTable.Columns.Add(myColumn1)


'Declare DataColumn tYear
Dim myColumn2 As New DataColumn("tYear")
myColumn2.DataType = System.Type.GetType("System.Int32")
'Add Column to table
myDataTable.Columns.Add(myColumn2)
'Get rid of duplicates
Dim myUC As New UniqueConstraint("UC",
myDataTable.Columns("tYear"))
myDataTable.Constraints.Add(myUC)

icnt = 0
For Each dr In dsAutos.Tables("tAutos").Rows
icnt = icnt + 1
ib = Val(dr("tBYr"))
i = ib
ie = Val(dr("tEYr"))
Do Until i = ie + 1
Try
myRow = myDataTable.NewRow()
myRow("ID") = icnt
myRow("tYear") = i
Console.WriteLine(myRow("tYear"))
myDataTable.Rows.Add(myRow)
'Me.ListBoxYear.Items.Add(CStr(i))
i = i + 1
Catch
i = i + 1
End Try
Loop
Next
Console.WriteLine(icnt)
Console.WriteLine("-----")

For Each dr In myDataTable.Rows
Console.WriteLine(dr("tYear"))
Next

Dim strExp, strSort As String

'Sort
strExp = "tYear > 1900"
strSort = "tYear"

'Use the Select method to find all rows matching the filter.
Dim drsort As DataRow() = myDataTable.Select(strExp, strSort,
DataViewRowState.Added)

For Each drsort In myDataTable.Rows
Console.WriteLine(drsort("tYear"))
Next

Your problem lies in for each.
It is wrong for two reasons:
- you try to use row array as an iterator (drsort)
- you should do for each on drsort rows array returned by Select and not on
table
Dim dr as DataRow
for each dr in drsort
Console.WriteLine(dr("tYear"))
Next
 

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