Debugging My Code

Howlleo

Member
Joined
Jan 14, 2008
Messages
13
Programming Experience
Beginner
If this is not the right place for this, please redirect me.
I'm a super-beginner at this - I'm taking a course in ASP.NET with only a basic C++ background. This is for my final project, which is due in two days.
So disclaimer: there is probably going to be a very dumb error in here, but I can't spot it.

The page is a registration page for a hypothetical college. Connection to database working just fine. I tested The first subroutine: it writes the class names to the array just fine. I tested the second subroutine - it reads the credits from the database fine and writes the line to prove it.

I am assuming that it writes to the same array as the previous function, and therefore there is now an array with the names of the classes and their credit value in memory.

Problem: I'm trying to pass the array to the function CreditTotal() to add up the total credits the student registers for. But I keep getting an error on the line
VB.NET:
TotalCredits = CreditTotal(CreditsArray(12,1))
that reads
"Unable to cast object of type 'system.single' to type 'system.object[,]'."

I've searched online and can't find something that addresses this.

Any ideas?

I'm pasting the main code below and attaching a word doc with the error code, since it doesn't allow the full .aspx page. Thanks.


VB.NET:
    Protected Sub RegisterButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim CreditsArray(12, 1) As Object
        ' call subroutine to assign each selected class to a 2-dimensional array
        AssignArray(CreditsArray)

        ' call subroutine to find the credits awarded for each class
        GetCreditValue(CreditsArray)
        
        ' Call function to total the credits registered and return the value
        Dim TotalCredits As Integer
        TotalCredits = CreditTotal(CreditsArray(12, 1))

    End Sub
    
    ' the subroutine to store the credit value of each course in the array
    Sub GetCreditValue(ByVal CreditsArray)
        Dim i As Integer
        For i = 0 To 12
                           
            Dim ClassDb As SqlConnection
            Dim cmdSelectClass As SqlCommand
            Dim dtrClass As SqlDataReader
        
ClassDb = New SqlConnection("Server=LGORDON\SQLEXPRESS;Integrated Security=True;database=LGordonTouroReg")
            cmdSelectClass = New SqlCommand("SELECT Credits FROM Class WHERE ClassID=@ClassID", ClassDb)
            cmdSelectClass.Parameters.AddWithValue("@ClassID", CreditsArray(i, 0))
            ClassDb.Open()
            dtrClass = cmdSelectClass.ExecuteReader()
            While (dtrClass.Read())
                CreditsArray(i, 1) = dtrClass(0)
            End While
            dtrClass.Close()
            ClassDb.Close()
            Response.Write("The value for " & CreditsArray(i, 0) & " is " & CreditsArray(i, 1) & "<br/>")
        Next i
    End Sub
    
    ' the function to total the credits registered for
    Function CreditTotal(ByVal CreditsArray(,)) As Integer
        Dim TotalCredits As Integer = 0
        For j As Integer = 0 To 12
    ' *** the problem line
            TotalCredits = TotalCredits + CreditsArray(j, 1)
        Next j
        Return TotalCredits
        
    End Function
    

    ' the subroutine to assign the value of each registered class to the first element of the array
    Sub AssignArray(ByVal CreditsArray)
        CreditsArray(0, 0) = DropDownList1.SelectedItem.Value
        CreditsArray(1, 0) = DropDownList2.SelectedItem.Value
        CreditsArray(2, 0) = DropDownList3.SelectedItem.Value
        CreditsArray(3, 0) = DropDownList4.SelectedItem.Value
        CreditsArray(4, 0) = DropDownList5.SelectedItem.Value
        CreditsArray(5, 0) = DropDownList6.SelectedItem.Value
        CreditsArray(6, 0) = DropDownList7.SelectedItem.Value
        CreditsArray(7, 0) = DropDownList8.SelectedItem.Value
        CreditsArray(8, 0) = DropDownList9.SelectedItem.Value
        CreditsArray(9, 0) = DropDownList10.SelectedItem.Value
        CreditsArray(10, 0) = DropDownList11.SelectedItem.Value
        CreditsArray(11, 0) = DropDownList12.SelectedItem.Value
        CreditsArray(12, 0) = DropDownList13.SelectedItem.Value
    End Sub


ERROR MESSAGE:

Unable to cast object of type 'System.Single' to type 'System.Object[,]'.
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.Single' to type 'System.Object[,]'.

Source Error:

Line 15: ' Call function to total the credits registered and return the value
Line 16: Dim TotalCredits As Integer
Line 17: TotalCredits = CreditTotal(CreditsArray(12, 1))
Line 18: ' Check to see if there are too many credits
Line 19: ' Disallow registration if there is an overload

Source File: C:******** 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx Line: 17

Stack Trace:

[InvalidCastException: Unable to cast object of type 'System.Single' to type 'System.Object[,]'.]
ASP.registrationpage_aspx.RegisterButton_Click(Object sender, EventArgs e) in C:********** 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:17
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921
 

Attachments

  • Server Error in.doc
    26.5 KB · Views: 16
Last edited by a moderator:
Since this is dealing with an asp webform, I moved your thread to the 'web form' section under 'asp'
 
VB.NET:
TotalCredits = CreditTotal(CreditsArray)
This passes the array to the method and not an item in the array like you did.
 
Back
Top