• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1943
  • Last Modified:

Unable to cast object of type 'System.Int32' to type 'System.Collections.IEnumerable'

trying to set up a pie chart of an application but I keep getting this error, the data that comes from the db are numbers.  what am I doing wrong here.

Unable to cast object of type 'System.Int32' to type 'System.Collections.IEnumerable'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Collections.IEnumerable'.

Source Error:

Line 66:
Line 67:
Line 68:         chInOut.Series(0).Points.DataBindXY("Scheduled", myDataTable.Rows(0)(0))
Line 69:         chInOut.Series(1).Points.DataBindXY("Check-In", myDataTable.Rows(0)(1))
Line 70:         chInOut.Series(2).Points.DataBindXY("Not Present", myDataTable.Rows(0)(0) - myDataTable.Rows(0)(1))

Private Sub FillPieCharts()
        Dim EventId As Integer = ddlSRPMonth.SelectedValue
        Dim SRpDay As String = ddlSRPDay.SelectedValue

        sql = "Select COUNT(intScheduleID), (Select COUNT(intSrpAttendId) from tblSRPAttendance where intEventId = " & EventId & ") from ModreScheduler.dbo.tblSchedule as s INNER JOIN ModreScheduler.dbo.tblTimeSlots as " _
            & "ts on ts.intTimeSlotID = s.intTimeSlotID where intEventDateID = " & SRpDay

        myDataTable = New DataTable
        myDataTable = getData(sql)

        chInOut.Series(0).Points.DataBindXY("Scheduled", myDataTable.Rows(0)(0))
        chInOut.Series(1).Points.DataBindXY("Check-In", myDataTable.Rows(0)(1))
        chInOut.Series(2).Points.DataBindXY("Not Present", myDataTable.Rows(0)(0) - myDataTable.Rows(0)(1))

        pnlView.Visible = True
    End Sub

Open in new window

1 Solution
The DataBindXY() method expects IEnumerable parameters, not a single item (in your case, you are passing an integer). The method expects 'several' integers, whereas you are passing one integer.

Look at the example below:

' Initialize an array of doubles
Dim yval As Double() = {2, 6, 4, 5, 3}

' Initialize an array of string
Dim xval As String() = {"Peter", "Andrew", "Julie", "Mary", "Dave"}

' Bind the double array to the Y axis points of the Default data series
Chart1.Series("Series 1").Points.DataBindXY(xval, yval)

Open in new window

Alfred A.Commented:
Try this:

chInOut.Series(0).Points.DataBindXY("Scheduled", CType(myDataTable.Rows(0)(0),Integer))


chInOut.Series(0).Points.DataBindXY("Scheduled", CInt(myDataTable.Rows(0)(0)))


chInOut.Series(0).Points.DataBindXY("Scheduled", Integer.Parse(myDataTable.Rows(0)(0)))

Alfred A.Commented:
Oops.  Ignore my previous post, I didn't realise it was casting a single item where it needs a collection and not a single item.  My bad. :-)
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

kdeutschAuthor Commented:

I am trying to make a simple pie chart that has 3 sections, one for scheduled, one for checked in, one for not present, the numbers for each of these comes from mydatatable.  how can I do this.
kdeutschAuthor Commented:
ok see what I was doing wrong.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now