0

I have an issue on my ASP.NET 4.0 application. The error appears when I call one of my subroutines in the App_Code folder. I call the subroutine when a button is clicked on the page.

Protected Sub imgBtnEmailReg_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgBtnEmailReg.Click
    Dim functions As New Functions
    Dim pidm As Integer = Convert.ToInt32(Session("BannerPidm").ToString)
    functions.sendStudentAccomLetter(pidm)
End Sub

After the button click the id number is sent to the App_Code/Functions.vb where my subroutine is:

Public Sub sendStudentAccomLetter(ByVal stuPidm As Integer)
...
End Sub

The error I get when calling the above function is:

"System.InvalidCastException: Specified cast is not valid. at Functions.sendStudentAccomLetter(Int32 stuPidm) in D:\www-sec-docs\DisabilityServices\App_Code\Functions.vb:line 259"

Line 259 is the sub signature. I'm not even sure if there is a cast occurring here. Can someone please explain what might be causing this. Thank you.

4

3 回答 3

0

您的 Session("BannerPidm") 值可能是不同的类型 - 方法声明不是强制转换错误的原因。尝试对 Session("BannerPidm") 值进行某种检查,例如:

If IsNumeric(Session("BannerPidm")) Then
   'Proceed
于 2013-09-09T19:35:04.070 回答
0

此链接显示了一些示例,您在尝试转换时会遇到错误:http: //bytes.com/topic/visual-basic-net/answers/514682-difference-between-cint-convert-toint32

尝试CInt()改用,看看是否还有问题。如果你这样做了,那么你可能想开始检查会话变量是什么,看看是否有更好、更标准化的方式来存储它。

于 2013-09-09T13:04:56.783 回答
0

很可能是转换为 Int32。在该行设置断点,然后查看Session("BannerPidm") 返回的值。肯定会发生强制转换,因为您正在从 Session 中检索,将其强制转换为字符串 ( .ToString()),然后将其转换为 Int32。

于 2013-09-09T13:05:16.697 回答