HighTechTalks DotNet Forums  

value of variables disappear

ASP.net ASP.net discussions (microsoft.public.dotnet.framework.aspnet)


Discuss value of variables disappear in the ASP.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Stefan Bosman
 
Posts: n/a

Default value of variables disappear - 12-13-2007 , 06:08 AM






Hi,

when i declare a variable in the "pageload event" or in the "Partial Class
_default"
my program will recognize the variable but it will loose the value.

example:

Partial Class _Default
Inherits System.Web.UI.Page
Dim conStr As String =
ConfigurationManager.ConnectionStrings("connString 1").ConnectionString

order_id = Session("session_orders")
Dim sqlstr_order As String = "SELECT * FROM Orders WHERE OrdersBaseID="
+ Str(order_id)
Dim sqladapter_order As New SqlDataAdapter(sqlstr_order, conStr)
ds_Order.Reset()
sqladapter_order.Fill(ds_Order, "Orders")

when i use msgbox(sqlstr_order) in an other code it will return an empty
string and loading data of this table will not work.

example:
ds_Order.Tables("Orders").Rows(0)("OrderGammaType" ) + "'"

everything works fine if i copy the code in the other sub every time i need
data from the table.

thanks in advance
(New dot net programmer)









Reply With Quote
  #2  
Old   
Hans Kesting
 
Posts: n/a

Default Re: value of variables disappear - 12-13-2007 , 08:31 AM






Stefan Bosman wrote on 13-12-2007 :
Quote:
Hi,

when i declare a variable in the "pageload event" or in the "Partial Class
_default"
my program will recognize the variable but it will loose the value.

example:

Partial Class _Default
Inherits System.Web.UI.Page
Dim conStr As String =
ConfigurationManager.ConnectionStrings("connString 1").ConnectionString

order_id = Session("session_orders")
Dim sqlstr_order As String = "SELECT * FROM Orders WHERE OrdersBaseID=" +
Str(order_id)
Dim sqladapter_order As New SqlDataAdapter(sqlstr_order, conStr)
ds_Order.Reset()
sqladapter_order.Fill(ds_Order, "Orders")

when i use msgbox(sqlstr_order) in an other code it will return an empty
string and loading data of this table will not work.

example:
ds_Order.Tables("Orders").Rows(0)("OrderGammaType" ) + "'"

everything works fine if i copy the code in the other sub every time i need
data from the table.

thanks in advance
(New dot net programmer)
I think that's because VB tricks you. When you use the same variable
name in some other method (after you have declared it inside some
method) then it is NOT the same variable but an inplicitly declared one
that has no relation whatsoever to the first one.
I think there is an "option strict" you can set somewhere to disable
this behaviour, so you can catch errors like this.

A variable declared inside some method is local to that method
(=unknown outside it). One solution is to declare that variable at
class level and set the value inside a Page_Load (or some other method
that is always executed before you want to use it).

Hans Kesting




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.