Oh No a Global Variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation? If
so, what is the code to make a global string variable, and where do I put it,
(before the namespace, of just under it)?????
 
You can't have variables outside of a class. Try it and you'll get a compile
error (I just tried). I would suggest having a config file that stores your
connection string, and maybe a singleton settings class that is responsible
for retrieving all of your settings. This way you are sure your entire app
is using the same values, and you only retrieve them in one place.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation? If
so, what is the code to make a global string variable, and where do I put it,
(before the namespace, of just under it)?????

The usual solultion for this is to have a static class of static methods or
properties that return the necessary "global" values.

static class Settings
{
public static string MyValue
{
get
{
return "MyValue";
}
}
}

Then you can use it like this :

{
string user = Settings.MyValue;
...
}

Joanna
 
I added a config file but it looks like xml code. Will this work for my win
form application?
 
I would go with a App.config file. That is exactly it's purpose, it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark
 
Yes. Like so:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="dsn" value="Server=(local);Database=XXXX;User
ID=XXXX;Password=XXXXXXXX" />

</appSettings>

</configuration>
 
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings' could
not be found (are you missing a using directive or an assembly reference?)


I also tried: string sConnString = configuration.appSetting["dsn"];
I get the error: The type or namespace name 'configuration' could not be
found (are you missing a using directive or an assembly reference?)

Here is the App.config code, I removed the password for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>

Mark White said:
I would go with a App.config file. That is exactly it's purpose, it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark


Mike L said:
This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation? If
so, what is the code to make a global string variable, and where do I put it,
(before the namespace, of just under it)?????
 
Mike

It would be: System.Configuration.ConfigurationSettings.AppSettings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];

Mike L said:
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings' could
not be found (are you missing a using directive or an assembly reference?)


I also tried: string sConnString = configuration.appSetting["dsn"];
I get the error: The type or namespace name 'configuration' could not be
found (are you missing a using directive or an assembly reference?)

Here is the App.config code, I removed the password for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>

Mark White said:
I would go with a App.config file. That is exactly it's purpose, it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark


Mike L said:
This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this
situation?
If
so, what is the code to make a global string variable, and where do I
put
it,
(before the namespace, of just under it)?????
 
Mike L said:
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings' could
not be found (are you missing a using directive or an assembly reference?)

That suggests you're missing the using directive:

using System.Configuration;
 
Hi Joanna.
What is a static class in C#? Please tell us... <vbg>

Anyways, I think would simply do this:

public class DataAccess
{
public static readonly string ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
}

Or better, provide a simple factory for connections.

public class DataAccess
{
private DataAccess() { }
private static readonly string connectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
public static SqlConnection Create()
{
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
return conn;
}
}

Happy Coding
- Michael S
 
Now I get this error:

An unhandled exception of type 'System.Configuration.ConfigurationException'
occurred in system.dll

Additional information: Unrecognized configuration section appSetting




Here is my code, password as be removed for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>



I have tried both, string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"]; and string
sConnString = ConfigurationSettings.AppSettings["dsn"];
I get the error on both.

Added info, if this helps. The call to string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"]; is on Load of
my Win Form named frmDataEntry, and the file name for storing the value is
named App.config



Mark White said:
Mike

It would be: System.Configuration.ConfigurationSettings.AppSettings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];

Mike L said:
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings' could
not be found (are you missing a using directive or an assembly reference?)


I also tried: string sConnString = configuration.appSetting["dsn"];
I get the error: The type or namespace name 'configuration' could not be
found (are you missing a using directive or an assembly reference?)

Here is the App.config code, I removed the password for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>

Mark White said:
I would go with a App.config file. That is exactly it's purpose, it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark



This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation?
If
so, what is the code to make a global string variable, and where do I put
it,
(before the namespace, of just under it)?????
 
it is called <appSettings> NOT <appsetting>

HTH

Ollie Riches

Mike L said:
Now I get this error:

An unhandled exception of type
'System.Configuration.ConfigurationException'
occurred in system.dll

Additional information: Unrecognized configuration section appSetting




Here is my code, password as be removed for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>



I have tried both, string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"]; and string
sConnString = ConfigurationSettings.AppSettings["dsn"];
I get the error on both.

Added info, if this helps. The call to string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"]; is on Load
of
my Win Form named frmDataEntry, and the file name for storing the value is
named App.config



Mark White said:
Mike

It would be:
System.Configuration.ConfigurationSettings.AppSettings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];

Mike L said:
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings'
could
not be found (are you missing a using directive or an assembly
reference?)


I also tried: string sConnString = configuration.appSetting["dsn"];
I get the error: The type or namespace name 'configuration' could not
be
found (are you missing a using directive or an assembly reference?)

Here is the App.config code, I removed the password for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>

:

I would go with a App.config file. That is exactly it's purpose,
it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark



This is for a Win form.

Currently I have several connection strings that are identical
through out
my application. Is a global variable the best choice for this situation?
If
so, what is the code to make a global string variable, and where do
I put
it,
(before the namespace, of just under it)?????
 
Michael S said:
What is a static class in C#? Please tell us... <vbg>

In C# 2.0, it's a class which is declared as being static. It is
implicitly sealed, has *no* constructors (private or public), and all
its members have to be static.
 
Once I corrected my App.config file to appSettings, it worked!!!

Thank you everyone!!



Ollie Riches said:
it is called <appSettings> NOT <appsetting>

HTH

Ollie Riches

Mike L said:
Now I get this error:

An unhandled exception of type
'System.Configuration.ConfigurationException'
occurred in system.dll

Additional information: Unrecognized configuration section appSetting




Here is my code, password as be removed for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>



I have tried both, string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"]; and string
sConnString = ConfigurationSettings.AppSettings["dsn"];
I get the error on both.

Added info, if this helps. The call to string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"]; is on Load
of
my Win Form named frmDataEntry, and the file name for storing the value is
named App.config



Mark White said:
Mike

It would be:
System.Configuration.ConfigurationSettings.AppSettings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];

I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings'
could
not be found (are you missing a using directive or an assembly
reference?)


I also tried: string sConnString = configuration.appSetting["dsn"];
I get the error: The type or namespace name 'configuration' could not
be
found (are you missing a using directive or an assembly reference?)

Here is the App.config code, I removed the password for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>

:

I would go with a App.config file. That is exactly it's purpose,
it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark



This is for a Win form.

Currently I have several connection strings that are identical
through
out
my application. Is a global variable the best choice for this
situation?
If
so, what is the code to make a global string variable, and where do
I
put
it,
(before the namespace, of just under it)?????
 
Oh.

I and was sitting hare having a laugh at Joanna (as she is seldom wrong).
Sorry Joanna and thanks Jon.

Happy Static
- Michael S
 

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

Back
Top