Thursday, March 29, 2012

Passing Values Between web forms pages

i am trying to implement passing values from my login webpage to another. but i get an error of "Specified cast is not valid."

i followed the program logic and I cant find the error that the debugger says that it is in the casting. I am following the example provided by Microsoft in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpassingservercontrolvaluesbetweenpages.asp

next, it is the code behind of both forms

1st webform

<---login.aspx---
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
Public ReadOnly Property UserId() As String
Get
Return Trim(txtUser.Text)
End Get
End Property

Private Sub lnkLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkLogin.Click

Server.Transfer("secondForm.aspx")

end sub

<----secondForm.aspx---
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web

Imports System.Web.Security
Imports System.Web.UI

Public Class secondForm

Inherits System.Web.UI.Page

Public userLogin As login

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim blnAddinProgress As Boolean

Dim strUsername As String

If Not IsPostBack() Then

userLogin = CType(Context.Handler, login)

strUsername = userLogin.UserId

<-----
The error that i got from the debugger is "Specified cast is not valid."

following this message, Do i have to change the casting or is there another error or piece of code that i am forgetting.

thanks in advanceYou might try the similar technique documented in this topic:

Passing Values Between Web Forms Pages
HI! the link that you provided me is the one that i used.

If you see the structure of the code is pretty simmilar to the one of the link. Do you know about other option?
the error could be related to I am using Forms authentications?

thank you very much for your help
>HI! the link that you provided me is the one that i used.

Ok. The URL that you provided is not the same as the one for the other article, so I was a little confused.

Here's a question: what kind of control is lnkLogin in your login page? It should be a LinkButton or just Button (not a Hyperlink).

So, assuming that you're using a LinkButton, the bad news is that I cannot reproduce your error. I had to add a line (which you probably have). This is what my target page code looks like:

 Public userLogin As login
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim blnAddinProgress As Boolean
Dim strUsername As String
If Not IsPostBack() Then
userLogin = CType(Context.Handler, login)
strUsername = userLogin.UserId
Response.Write(strUsername)
End If
End Sub

0 comments:

Post a Comment