Thursday, March 29, 2012

Passing values between pages using Forms Authentication

I want to know how can i pass a value from my database to another page using Forms Authentication.

In this example:

1Sub Logon_Click(ByVal senderAs Object,ByVal eAs EventArgs)Handles Submit1.Click23Dim objConnectionAs Data.SqlClient.SqlConnection4Dim objCommandAs Data.SqlClient.SqlCommand5Dim usernameAs String6 Dim passwordAs String7 Dim strSQLQueryAs String8 Dim RecordCountAs Integer910 username = userTextBox.Text11 password = passTextBox.Text1213If Len(Trim(username)) > 0Then14 objConnection =New Data.SqlClient.SqlConnection("Data Source=localhost;" _15 &"Initial Catalog=web_dados;User Id=sa;Password=platinum;" _16 &"Connect Timeout=15;")1718 strSQLQuery ="SELECT * FROM conserto WHERE web_user ='" & username & "' AND web_pass = '" & password & "' "19 objCommand = New Data.SqlClient.SqlCommand(strSQLQuery, objConnection)202122 Try23 objConnection.Open()24 RecordCount = CInt(objCommand.ExecuteScalar())25 Catch ex As Exception26 Finally27 objConnection.Close()28 End Try2930 If RecordCount = 0 Then31 Msg.Text = "Identificação de conserto ou código de acesso inválido."32Else33 FormsAuthentication.RedirectFromLoginPage _34 (userTextBox.Text, Persist.Checked)35End If3637 objConnection.Close()38End If394041 End Sub42

I want to store the value of the field named <numero> of the database, and when the user authenticates, i want to print that variable in the default.aspx page.

You can use a Session variable to do so. If you are using ASP.NET 2.0, then you could use a Profile object too.

Does this help?

Regards


i've just tried to use sessions, but when the user autheticates and are redirected to the default.aspx page, it lost the session value.

1 username = userTextBox.Text2 password = passTextBox.Text3 Session("USERNAME") = username45If Len(Trim(username)) > 0Then6 objConnection =New Data.SqlClient.SqlConnection("Data Source=localhost;" _7 &"Initial Catalog=web_dados;User Id=sa;Password=platinum;" _8 &"Connect Timeout=15;")910 strSQLQuery ="SELECT * FROM conserto WHERE web_user ='" & username & "' AND web_pass = '" & password & "' "11 objCommand = New Data.SqlClient.SqlCommand(strSQLQuery, objConnection)121314 Try15 objConnection.Open()16 RecordCount = CInt(objCommand.ExecuteScalar())17 Catch ex As Exception18 Finally19 objConnection.Close()20 End Try2122 If RecordCount = 0 Then23 Msg.Text = "Identificação de conserto ou código de acesso inválido."24Else2526 FormsAuthentication.RedirectFromLoginPage _27 (userTextBox.Text, Persist.Checked)

in this code in the default.aspx it doesn't display any value.

1Sub Page_Load(ByVal SrcAs Object,ByVal eAs EventArgs)2 userLabel.Text = Session("USERNAME")3End Sub4

why?

0 comments:

Post a Comment