Showing posts with label second. Show all posts
Showing posts with label second. Show all posts

Saturday, March 24, 2012

Passing Variables

This is from an earlier thread:

www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=133432

I am getting zero for my variable the second time it accesses the subrountine. It is obivous to me that its getting initialized every time, thus reseting it to zero.

How can I initialize the variable once and not have it re-initialized every time? What is the syntax?

Thanks for the help,

-JJust a swag, but are you checking "IsPostBack" in your PageLoad before initializing the variables?

Another thing to remember is that no variables survive a roundtrip to the client. You have to save them somewhere (viewstate, session), and restore their values each time.
I am not sure what you are asking? I am checking for IsPostBack in Page Load, but the variable that is in question is not declared up there, but within the subroutine. When I used to program in PICK it was always good practice to declare your variables at the top of the program and you never had to declare them again. Seems in ASP.NET you, its geared for option explicit, which makes it difficult sometimes when you have to declare everytime you want to use the same variable more than one place in the program.

I did think of this earlier although, but I was getting an undeclared error when I intialized it in the page load.

How do you use the viewstate to handle something like this?

Thanks for you help so far,

-J
In VB.NET or C#.net, you can declare variables at the "class" level. For example:


Public Class MyPage
Inherits System.Web.UI.Page

Protected dateBegin As System.DateTime
Protected dateEnd As System.DateTime
etc.

These will be accessable from anywhere in your code-behind, or from the ASPX page that inherits from the code-behind. Typically, then, within a function or sub, you can then use these without declaring them, but they will, of course, need to be initialized first. It is good practice to _only_ use this kind of variable for things that will be needed throughout the class. For variables that are only used within a function, you declare them withing the function. These go out of scope when you exit the function.

Viewstate is pretty simple to use. To store something there:
Viewstate("dateBegin") = dateBegin

To get it back, you need to cast the type:
dateBegin = CType(Viewstate("dateBegin"), System.DateTime)

========

Typically, with your begin/end variables you might do something like:


In page-load:
If Not IsPostBack Then
dateBegin = Date.Today()
dateEnd = dateBegin
Viewstate("dateBegin") = dateBegin
Else
dateBegin = CType(Viewstate("dateBegin"), DateTime)
dateEnd = Date.Today()
End If

Now, if you declared the two dates as "Protected... " at the class level, you can access them in any function that runs after PageLoad in the processing cycle. (and postback handlers DO run after PageLoad).

HTH,
Wow, thank you so much for this explanation. I've also been searching and couldn't find why my variable values where disappearing.

Wednesday, March 21, 2012

passing variables to new aspx page using hyperlink column from a datagrid

I am trying to get the value of the hyperlink column to the second aspx page to use it in a sql query. I have it passing to the new page. I don't know how to get/receive it in the new page. Any help would be appreciated.

<Columns>
<asp:TemplateColumn ><ItemTemplate>
<asp:HyperLink runat="Server" NavigateUrl='<%# "../IPDP/ViewObjective.aspx?PlanID=" & DataBinder.Eval(Container.DataItem, "IPDP_PLAN_ID") & "&ObjID=" & DataBinder.Eval(Container.DataItem, "IPDP_OBJ_ID")%>' Text="VIEW" ID="hyperObj"/>
</asp:HyperLink></ItemTemplate></asp:TemplateColumn

example on other page request("PlanID")
Maybe I'm misunderstanding you. This is what I have and the error it's giving me.

From page1:
<asp:TemplateColumn HeaderText="Select Ticket">
<ItemTemplate>
<asp:HyperLink id="HyperLink1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Number")%>' NavigateUrl='<%# "ticketsearch_db2a.aspx?Number=" & DataBinder.Eval(Container, "DataItem.Number")%>' />
</ItemTemplate>
</asp:TemplateColumn
On page2:

Dim TicNumber as string
TicNumber = request("Number")
Dim vNumber as String
Dim vName as String
Dim vDtissue as String
Dim vrace as String
Dim vsex as String
Dim vDlNum as String
Dim voffcode as String
Dim vspact as String
Dim vsppost as String
Dim vDtCourt as String
Dim vCity as String
Dim vStreet as String
Dim vCitype as String
Dim vCourt as String
Dim strConn as string = "server='sma18'; user id='sdasch'; password='ilovemynewhouse'; database='bluegrass"& _
"'"
Sub DoQuery(Source as Object, E as EventArgs)

Dim MySQL as string = "SELECT [Tickets].* FROM [Tickets] WHERE [Tickets].[number] = '" & TicNumber & "'"
Dim MyConn as New SQLConnection(strConn)
Dim ds as DataSet=New DataSet()
Dim Cmd as New SQLDataAdapter(MySQL,MyConn)
Cmd.Fill(ds,"Tickets")

'These lines assign the data to a variable, which is then assigned to the text property of a literal control(litEmps)
Dim dr As DataRow
For Each dr In ds.Tables("Tickets").Rows
vNumber+=dr("vnumber")
vName+=dr("first") & " " & dr("middle") & " " & dr("last") & " " & dr("suffix")
vDtissue+=dr("dtissue")
vrace+=dr("race")
vsex+=dr("sex")
vDlNum+=dr("dlnum")
voffcode+=dr("offcode")
vspact+=dr("spact")
vsppost+=dr("sppost")
vDtcourt+=dr("dtcourt")
vCity+=dr("city")
vStreet+=dr("street")
vCitype+=dr("citype")
vcourt+=dr("court")
Next
number1.text=vNumber
name1.text=vName
dtissue1.text=vdtissue
race1.text=vrace
sex1.text=vsex
DlNum1.text=vdlnum
offcode1.text=voffcode
spact1.text=vspact
sppost1.text=vsppost
Dtcourt1.text=vdtcourt
Street1.text=vstreet

Error:
Compiler Error Message: BC30188: Declaration expected.

Line 7: Dim TicNumber as string
Line 8: TicNumber = request("Number")
you can try Request.QueryString.Get("Number") for line 8
Thanks for trying. That gives me the same error.

I have <%=Request("Number")%> in the HTML portion of the code and that seems to give me the Number printed on the screen. I just need to figure out how to get it into a variable to use in the sql statement.

If you have any other ideas, I would appreciate them.

Again, Thanks for your help.

Follow this logic...

Function getRequestID()
Dim ID as INTEGER
ID = Request.QueryString("ID")
return ID
END FUNCTION

Where Function is called...
Page.aspx?ID=<%=getRequestID()%>

Passing variables vs session variable

what is the advantage of passing the variable in the url vs storing the variable in a session variable and retrieving it in the second webpage you are accessing?

The variable that you will pass using the QueryString will be visible to the user hence not secured. The value that you put in the Session is not available to the user as it is on the server side and hence more secured.


performance wise does it matter?


Just my 2 cents...

I remember in college the professors would always harp upon using the Session responsibly. Most of the professors were older and came from backgrounds where memory was used sparingly. So, if you were cramming loads of data into the Session (back in the day), you'd maybe begin to experience some bad effects because of it. Accordingly it was advised to use the Session sparingly.

However, it seems to me that things have changed a little. We have more memory and it's faster than ever; the average joe-blow user has more bandwidth than ever. Consequently I don't think people worry about using the Session too much as much.


Yes, performance wise, session variables take up memory on the server so using too many sessions can affect the overall performance of the site. I use them a lot and haven't seen too much of a performance issue, you must have to use a lot of them to affect memory. Or maybe sticking arrays into sessions would start to affect things. Good luck, Mike


there are a few ways of passing variables around:

Single page Postbacks:

1) Use ViewState. It's simple, encrypted. Only problem is too much will slow the clients computer down

2) Use hidden <asp:labels />. Pretty much the same approach as the veiw state.


Navigating from page-> page

1) Query String. Yea, they might see some ID's at the top.

2) you can "Submit" to a new page with ASP.NET 2.0, which allows you to use values from your original page.

3) You can use session. But I would advise against it. Session variables should be used to house things that are important for the user, like: ID, UserName, etc. I would never want to see a dataTable, Array, etc. stored in the session.