Showing posts with label system. Show all posts
Showing posts with label system. 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 files

Hey All :)

Im new to asp.net, but come from a background of php so all my asp is in C#.

Now i have a ASP.NET cms system that i am currently making changes to. Now what i have is filename.aspx which up the top calls header.aspx, and i am trying to pass a string from filename.aspx.cs to header.aspx.cs (the name of the header graphic i want to set).


In php i would just set the variable, and in the header file i would call the variable in via global $varName; but in asp this doesnt seem to be working (go figure :p)

Hope it makes sense, all feedback appreciated.

1. you can pass it through the QueryString -

perhaps while you redirect to header.aspx do something like response.redirect("header.aspx?imgfile=" + theVariableHoldingName);
and in the header.aspx.cs do something like (in the Page_Load): string nameOfImage = Rquest.QueryString("imgfile");

2. you can use Session also:

in filename.aspx - Session.Add("imgfile",theVariableHoldingName);
and in header.aspx - string nameOfImage = Session["imgfile"];


ben14:

In php i would just set the variable,

just set the session object where you would set the variable as.. Session["myData"] = value

ben14:

and in the header file i would call the variable in via global $varName

and get the value of that session object where you would call the variable in via global $varName as... myValue = Session["myData"]

dont forget to cast the value... as Session returns an Object..

hope it helps./.


thanks for that :) should work fine.

cheers