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

0 comments:

Post a Comment