Showing posts with label byval. Show all posts
Showing posts with label byval. Show all posts

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?

Monday, March 26, 2012

Passing variable from Sub to Sub

How do I pass the value to

Public Sub DeliverFile(ByVal Page As System.Web.UI.Page, _
ByVal Data() As Byte, ByVal Type As String, _
ByVal Length As Integer, _
Optional ByVal DownloadFileName As String = "")

Suppose I have a sub that get the connection as below?

sub connect(sender As Object, e As EventArgs)
dim cnn as string = "dsn=fyp; uid=root; pwd="
dim oconn as new odbcconnection(cnn)
dim sql as string
sql = "Select * From ReportSubmission Where StudentID = 'WeiSiong';"
dim cmd as odbccommand
cmd = new odbccommand(sql, oconn)
oconn.open
cmd.executereader
oconn.close
end sub
You have to explain yourself. What values are you getting from database, what kind of data types are they. What do want to do with these values and your method?
To get the data and put it into a dataset (a collection of data that ASP.NEt controls natively bind to) you would use the following function:

Function GetData(ByVal StudentID As String) As System.Data.DataSet
Dim connectionString As String = "Insert your connection string here"
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
Dim query As String = "SELECT * FROM ReportSubmission WHERE StudentID = @.StudentID"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = query
dbCommand.Connection = dbConnection

Dim paramSID As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
paramSID.ParameterName = "@.StudentID"
paramSID.Value = StudentID
paramSID.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(paramSID)

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim ds As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return ds
End Function

Then, in order to access the dataset within your code you would call the function as follows


dim yourdata as string = GetData("WeiSiong").tables(0).rows(0).item("Name of column in the database that you want to read from")