How to declare a variable in the global scope?

M

Miguel Dias Moura

Hi Kevin,

I am not sure. If it is like that u mean that the change of value I do
in my script on page_load doesn't change the default value?

Thanks,
Miguel
 
J

Juan T. Llibre [MVP]

Hi, Miguel.

Kevin is right.

The web.config file's contents are loaded
into memory into the current AppDomain.

As soon as an Application starts, the configuration file
is loaded into memory, its contents are parsed and calls
are made to the relevant methods to register the objects
described in the file.

re:
the change of value I do in my script on page_load doesn't change the
default value?

No.

It only changes the variable instance's value.
The default value ( declared in web.config ) is unchanged.

You'd have to modify the web.config file itself,
in order to change the value of a variable set in it.

As soon as you save the changes to the new web.config,
the file will be reloaded and the changes registered.



Juan T. Llibre
===========
 
K

Kevin Spencer

Hola, Miguel,

Configuration values are read-only.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
M

Miguel Dias Moura

Hi,

This task it getting endless and really confusing :)

What I need seems really simple...

On CommandText used in the dataset I want to use the SQL code include on
string queryDocuments which is build on page_load.

Sorry, but I am completely lost...once again.

Thanks,
Miguel
 
J

John Saunders

Steve C. Orr said:
I used to think that but then I heard otherwise from somewhere...

I thought it was loaded once per AppDomain? In fact, isn't this what the
configuration API does in general, regardless of whether it's ASP.NET or a
Windows Service?

I once tried to get a Windows Service to re-read its config file by using
pause and continue, and it didn't work. I came to believe that I'd need to
start a new AppDomain in order to get a fresh read of the config file. I no
longer recall where I heard that the new AppDomain would do the trick, and
didn't try it out.

John Saunders
 
J

Juan T. Llibre [MVP]

re:
I used to think that but then I heard otherwise from somewhere.

Please tell whomever told you that, that he/she is wrong.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/CachingArchch3.asp

"A good example of this is the way the .NET Framework
handles its configuration files (Web.config and Machine.config),
which are loaded into memory as soon as they are updated,
causing the application to use the new configuration data
as soon as it is available."

HTH...



Juan T. Llibre
===========
 
J

Juan T. Llibre [MVP]

Steve was misled.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/CachingArchch3.asp

"A good example of this is the way the .NET Framework
handles its configuration files (Web.config and Machine.config),
which are loaded into memory as soon as they are updated,
causing the application to use the new configuration data
as soon as it is available."



Juan T. Llibre
===========
John Saunders said:
Steve C. Orr said:
I used to think that but then I heard otherwise from somewhere...

I thought it was loaded once per AppDomain? In fact, isn't this what the
configuration API does in general, regardless of whether it's ASP.NET or a
Windows Service?

I once tried to get a Windows Service to re-read its config file by using
pause and continue, and it didn't work. I came to believe that I'd need to
start a new AppDomain in order to get a fresh read of the config file. I
no longer recall where I heard that the new AppDomain would do the trick,
and didn't try it out.

John Saunders
 
J

John Saunders

Juan T. Llibre said:
Steve was misled.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/CachingArchch3.asp

"A good example of this is the way the .NET Framework
handles its configuration files (Web.config and Machine.config),
which are loaded into memory as soon as they are updated,
causing the application to use the new configuration data
as soon as it is available."

This talks about web.config and machine.config. I know that in the case of
web.config, the reason that the changes are loaded as soon as they are
available is that the application is restarted in a new AppDomain. This is
consistent with the idea that the configuration information loads once per
AppDomain, as though there were a single, static instance of the
configuration.

John Saunders
 
J

Juan T. Llibre [MVP]

Absolutely.

Saving either web.config or global.asax files
causes the restart of the application into a new AppDomain.

Saving the machine.config file will cause
a restart of *all* ASP.NET applications
on the server involved.

And, yes, the configuration information
loads once per AppDomain



Juan T. Llibre
===========
 
M

Miguel Dias Moura

Hi,

What a discussion this became.
So when I change the value of the variable in my script it doesn't
change the value in web.config file and so the global value is still the
same?

This is what it seems to be happening.

Anyway, maybe is there a way to solve this or maybe the solution is
completely different from using web.config?

Sorry, by I am starting with ASP.NET and I am working on this for 2
weeks and this is the last detail to end this project.

Thanks,
Miguel
 
K

Kevin Spencer

Use Application Cache.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
J

Juan T. Llibre [MVP]

re:
What a discussion this became.

It hasn't been a "nasty" discussion, though... :)
It's great when we can exchange differing points of view,
because then there's a chance for the truth to emerge.

re:
So when I change the value of the variable in my script it doesn't
change the value in web.config file and so the global value is still the same?

I thought we had covered that situation.

Here's the quote from my message to you :
---------------

You are retrieving/setting the query's value twice.

Once with:
CommandText='<%# System.Configuration.ConfigurationSettings.AppSettings("queryDocuments") %>'

and you retrieve it again with
Dim queryDocuments As String = System.Configuration.ConfigurationSettings.AppSettings("queryDocuments")

Is it possible that the first time you retrieve it
sets the value for the CommandText permanently ?

Why are you retrieving that value twice,
and assigning the same value to two different variables ?
( CommandText and queryDocuments )


If you have a permanent value for CommandText,
then nothing you do with "queryDocuments", later,
will affect the value for "CommandText".




Juan T. Llibre
===========
 
K

Kevin Spencer

Let me expand on that. You can store an initial value in a config file, and
when the app starts, assign that value to a Cache variable. After that, you
use only the Cache variable, which you may change at will.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
M

Miguel Dias Moura

Hi,

Yes it has been a great discussion. I have been learning a lot. :)

As I told you I am new to ASP.NET and at last post I got completely
lost.

Please focus on the problem I have in hands. :)
It's just that receiving all kind of general ideas is great because I
can learn but after a while I get lost and I try all kind of things but
I never get this right and working.

Here is my entire code. Maybe it's easier to get a view of it.

<%@ Page Language="VB" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls"
Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,culture=neutral"
%>
<MM:DataSet
id="dsDocuments"
runat="Server"
IsStoredProcedure="false"
ConnectionString='<%#
System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_conBonsAlunos")
%>'
DatabaseType='<%#
System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_conBonsAlunos")
%>'
CommandText='<%#
System.Configuration.ConfigurationSettings.AppSettings("queryDocuments")
%>'
Debug="true"
</MM:DataSet>
<MM:pageBind runat="server" PostBackBind="true" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import NameSpace="System.Text" %>

<script runat="server">

Sub Page_Load(sender As Object, e As System.EventArgs)

Response.Write(System.Configuration.ConfigurationSettings.AppSettings("queryDocuments"))
Dim queryDocuments As String =
System.Configuration.ConfigurationSettings.AppSettings("queryDocuments")
If Request.QueryString("pesquisar") Is Nothing Then
queryDocuments = "SELECT * FROM dbo.documents"
Else
queryDocuments = ""
Dim keywords as string()
keywords = Request.QueryString("pesquisar").Split(CChar(" "))
Dim i As Int32
For i = 0 To keywords.Length
queryDocuments += String.Format("CONTAINS (*, '{0}') ",
keywords(i))
If i + 1 = keywords.Length Then
i = keywords.Length
Exit For
End If
queryDocuments += " AND "
Next
queryDocuments = String.Format("SELECT * FROM dbo.documents WHERE
") & queryDocuments
End If
End Sub

Sub searchDocuments(sender As Object, e As System.EventArgs)
Server.Transfer("material-didactico.aspx?pesquisar=" &
Request.Form("pesquisar_keywords"))
End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
..
..
..

WEB.CONFIG:

<appSettings>
<add key="MM_CONNECTION_HANDLER_conBonsAlunos" value="sqlserver.htm"
/>
<add key="MM_CONNECTION_STRING_conBonsAlunos" value="Persist Security
Info=False;Data Source=(local);Initial Catalog=dbBonsAlunos;User
ID=sa;Password=" />
<add key="MM_CONNECTION_DATABASETYPE_conBonsAlunos" value="SQLServer"
/>
<add key="MM_CONNECTION_SCHEMA_conBonsAlunos" value="" />
<add key="MM_CONNECTION_CATALOG_conBonsAlunos" value="" />
<add key="queryDocuments" value="SELECT * FROM dbo.documents"/>
</appSettings>

Thanks once again,
Miguel
 
J

Juan T. Llibre [MVP]

As near as I can figure it out, what's happening
is that you're binding the MM datagrid CommandText
variable to the web.config queryDocuments key.

The CommandText never gets updated, because when you later
process the queryDocuments variable, that does not change the
value of the web.config appSetting to which the MM Datagrid's
CommandText was bound earlier.

You need to pass the *new* contents of the queryDocuments
variable to the MM Datagrid, after you process the changes to it.

I don't know enough about how the MM Datagrid takes variables
so that I could recommend you a course of action.

If you can pass the queryDocuments variable to the MM Datagrid
*after* you process that variable, that should work OK.

You might have to ask in a MacroMedia ng to find that out.

i.e., if you could place the MM Datagrid *after* you process the
queryDocuments variable, and pass the new value with something like :

CommandText='<%# queryDocuments %>'

then, I'm pretty sure it'd work.

But, like I said, I don't know enough about
the MM Datagrid to tell you if that's possible.




Juan T. Llibre
===========
 
G

Guest

Hi...

from you post what i understood is..
you have a value (some qrystring) that will be changed by the user..after
changing ..the changed value should be used...is it ?

if my point is correct.....you can maintain the important values(dynamic
values) with in viewstate..

-raj
 
M

Miguel Dias Moura

People,

Sometimes the solution is simpler then we think.

I just used this:


Sub Page_Load(sender As Object, e As System.EventArgs)
If Request.QueryString("pesquisar") Is Nothing Then
queryDocuments = "SELECT * FROM dbo.documents"
Else
...

Now queryDocuments is available everywhere in the page and I can use:
CommandText='<%# queryDocuments %>'

It's solved.

Thanks to Everybody,
Miguel
 
J

Juan T. Llibre [MVP]

I'm happy to see that the suggestion to use
CommandText='<%# queryDocuments %>'
worked, Miguel.

Congratulations on figuring out that
Dim queryDocuments as String = String.Empty
was needed in order to be able to use it.




Juan T. Llibre
===========
 
R

Ravichandran J.V.

Declare it as global or static or put it in the global.asax file as an
Application object -

Application("query")= "Whatever"

or put it under the appSettings section in web.config file.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top