[RESOLVED] [2005] Class doesnt store values
hi all!!
wonder why my class doesn't store the values???
the class
Code:
Imports Microsoft.VisualBasic
Public Class Strings
Private _user As String = ""
Property User() As String
Get
Return _user
End Get
Set(ByVal value As String)
_user = value
End Set
End Property
End Class
the login:
Code:
Sub dologin(ByVal Source As Object, ByVal E As EventArgs)
Dim strConn As String = (ConfigurationManager.ConnectionStrings _
("GesQualConnectionString").ConnectionString)
Dim MySQL As String = "Select ID, Utilizador , Pass ," & _
"AlterarPass from Users Where Utilizador = '" & _
TxtUser.Text & "' and Pass = '" & Uteis.GeraHash(TxtPass.Text) & "'"
Dim MyConn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand(MySQL, MyConn)
MyConn.Open()
objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
If Not objDR.Read() Then
LblResposta.Text = "<h1><b>User / Pass Inválidos !</b></h1></p>"
While objDR.Read()
strNome = objDR("Utilizador")
intCode = objDR("ID")
bprimeirologin = objDR("AlterarPass")
End While
Session("ID") = intCode
Session("Utilizador") = strNome
Dim clsStrings As New Strings
clsStrings.User = strNome
objDR.Close()
MyConn.Close()
Else
objDR.Close()
MyConn.Open()
If bprimeirologin Then
Response.Redirect("~\MudaPass.aspx")
Else
Response.Redirect("~\Default.aspx")
End If
End If
End Sub
and the call:
Code:
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim ClsStrings As New Strings
Label1.Text = ClsStrings.User
End Sub
Re: [2005] Class doesnt store values
Code:
Dim clsStrings As New Strings
The above code in ur program automatically deletes the previous value. And also I can't understand what u trying to do. Because this is not declared public. Explain the code for exact answer.
Re: [2005] Class doesnt store values
thanks senthilkumartd!!!
i need to store some strings to use in other pages!
provided by the login page, such as UserName and so on!
wanted to keep them in the strings class
and get them when i want to use them
Re: [2005] Class doesnt store values
ok it is public now but still doesn't work:
Code:
Public Class Strings
Private Shared _user As String = ""
Public Shared Property User() As String
Get
Return _user
End Get
Set(ByVal value As String)
_user = value
End Set
End Property
End Class
Re: [2005] Class doesnt store values
Your object for the Strings class are only stored for the life time of the dologin method. If you want to access the object with another method you will have to instantiate the object in the class level of whatever class dologin resides in. that way any method in the class that dologin reside in can access all public methods of the Strings class.
Re: [2005] Class doesnt store values
This is a surreal thread. I see a class designed to store a string. Almost redundant. And then nobody mentions session variables.
Motorui, store the username in a session variable.
Session("username") = strNome
Then in another page,
Label1.Text = Session("username").ToString()
Re: [2005] Class doesnt store values
Kudos mendhack... for something this simple, session variables is the wya to go. Even if the data was complex, creating the class, setting the properties, then STORE THE CLASS IN THE SESSION VARIABLE would be the way to go. Simply putting the data into a class doesn't make it appear on the other side. It needs to be stored and sent, jsut like anything else.
-tg
Re: [2005] Class doesnt store values
thanks all!
i will use session
sorry about this but I'm still noob!
hehehe
Re: [RESOLVED] [2005] Class doesnt store values
one more question
how to add select statement value to session??
Re: [RESOLVED] [2005] Class doesnt store values
i tried this it does the login just fine but doesn't store the user name in session:
Code:
Dim strConn As String = (ConfigurationManager.ConnectionStrings _
("GesQualConnectionString").ConnectionString)
Dim MySQL As String = "SELECT UH ," & _
"Utilizador, LControl, CheckIn, Placa, LostFound, Admin " & _
" FROM Users Where Utilizador = '" & _
TxtUser.Text & "' and Pass = '" & Uteis.GeraHash(TxtPass.Text) & "'"
Dim MyConn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand(MySQL, MyConn)
MyConn.Open()
objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
If Not objDR.Read() Then
LblResposta.Text = "<h1><b>User / Pass Inválidos !</b></h1></p>"
While objDR.Read()
strNome = objDR("Utilizador")
intCode = objDR("ID")
End While
objDR.Close()
MyConn.Close()
Else
objDR.Close()
MyConn.Open()
Session("User") = strNome
If bprimeirologin Then
Response.Redirect("~\MudaPass.aspx")
Else
Response.Redirect("~\Default.aspx")
End If
End If
End Sub
Re: [RESOLVED] [2005] Class doesnt store values
1. Don't put SQL statements in session variables. You're better off creating a stored procedure that does this SELECT for you. You simply pass the username to it.
2. You've assigned strNome in the 'if' portion of your 'if-then-else' block, but you attempt to access it in the 'else' portion. It's not going to work, since only the 'if' portion or the 'else' portion will be executed, but not both. From your code I see that Utilizador is the same as TxtUser.Text, so you should say:
Session("User") = TxtUser.Text
And do this before the if block.
Re: [RESOLVED] [2005] Class doesnt store values
i started with user name just to test it! i need to retrieve to session other
items in the database!
i will try with a stored procedure!
and post here the result
thanks