value of variables disappear

S

Stefan Bosman

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("connString1").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)
 
H

Hans Kesting

Stefan Bosman wrote on 13-12-2007 :
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("connString1").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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top