Showing posts with label sub. Show all posts
Showing posts with label sub. Show all posts

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")

Saturday, March 24, 2012

Passing variables between sub procedures

I am currently coding in VB.Net.

I have used the folloiwng code to retrieve a variable value from a sending aspx page in the recieveing aspx page within the Sub Page_Load procedure:

Dim UsernameRecd as String
UsernameRecd = Context.Items("Username")

Which works fine.

However within the recieving aspx page l have another sub procedure based upon a click event on a button web control which also needs the above variable value but l cannot seem to retian the above value once the user clicks the button.

Is there a way to retrieve the variable value agian or do l have to store (and retrieve) the value once the page loads?

Help.

Parm.Store (and retrieve) the value

MajorCats