none
C# Global Variables RRS feed

  • Question

  •         public class SomeGlobalVariables
            {
                public string ConnectionString = "Driver={Adaptive Server Enterprise};server=***;port=***;db=***;uid=" + fLoginUsername + ";pwd=" + fLoginPassword + ";";
            }
    
    
            public string fLoginUsername
            {
                get { return txtUsername.Text; }
            }
    
            public string fLoginPassword
            {
                get { return txtPassword.Text; }
            }

    A few questions:

    Where should I put class SomeGlobalVariables ? Can I put it under a child form (public), or should I need to put it under MainForm?

    Is there a way to put textbox input value as part of Global Variable? In this case, textbox value will be part of string UsernameAndPassword

    How should I write it in order to have global Connecting String?

    Thanks.



    • Edited by VA_er Tuesday, March 12, 2019 3:06 AM
    Tuesday, March 12, 2019 2:52 AM

Answers

  • I think I understand now.

    Yes it can. But you must remember to prefix ConnectionString with the class name (SomeGlobalVariables) because it is static.

    public class FormLogin : Form
    {
       // Other code goes here.
    
       // Some code to set the connection string.
       void SomeMethod()
       {
          Username = txtUsername.Text;
          Password = txtPassword.Text;
    
          // Remember to put 'SomeGlobalVariables' in front of
          // the static variable.
          SomeGlobalVariables.ConnectionString = "Driver={Adaptive Server Enterprise};server=*****;port=*****;db=*****;uid=" + Username + ";pwd=" + Password + ";";
    
       }
    }
    
    
    public FormChild2 : Form
    {
       // Other code here.
    
       void SomeOtherMethod()
       {
          // We can get the connection string like this.
          // 'SomeGlobalVariables' goes in front again.
          string connection = SomeGlobalVariables.ConnectionString;
       }
    }

    • Marked as answer by VA_er Tuesday, March 12, 2019 1:07 PM
    Tuesday, March 12, 2019 4:29 AM
  • Thanks for bring up the term app.config, as a newbie to Visual Studio, I will look into it.

    However, I am not sure if it works for my case: my Visual Studio is installed in local machine, which the application will be used on Desktop virtualization. That being said, I need to build the application on local desktop, then move the application to Desktop virtualization. 

    Only in Desktop virtualization, the application can reach to ODBC database, I am not sure how to connect to database when working on app.config.

    IMO, what you have considered to be the correct path concerning the access of the connectionstring globally is truly not the optimal approach. 

    https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/

    <copied>

    Executable–hosted app.

    These apps have two configuration files: a source configuration file, which is modified by the developer during development, and an output file that is distributed with the app.

    When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.

    Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app. 

    <end>

    myApp.exe.config.is the runtime config file that .NET looks for to obtain config information, like the connectionstring, that must sit at the same location as the myApp.exe

    Secondly, you seem to have not understood that a database has credentials that must be used for a program to login to the database and use it, which is not unlike using credentials to login to the Windows O/S and use it. 

    Virtulization has nothing to do with how .NET expects to find the location of the runtime config file.

    To be honest, all you have done is brought VBA yippee ki yay wild west cowboy skills to C# .NET. You are still talking ODBC, which is COM,  when you should be talking ADO.NET

    • Marked as answer by VA_er Tuesday, March 12, 2019 3:54 PM
    Tuesday, March 12, 2019 3:30 PM

All replies

  • You can put SomeGlobalVariables wherever you want, but by convention it would normally go in its own source file, SomeGlobalVariables.cs.

    There is nothing to stop you putting the username and password into SomeGlobalVariables, as long as they are static (of course).

    Tuesday, March 12, 2019 3:03 AM
  • You can put SomeGlobalVariables wherever you want, but by convention it would normally go in its own source file, SomeGlobalVariables.cs.

    There is nothing to stop you putting the username and password into SomeGlobalVariables, as long as they are static (of course).

    Thanks, for above post's sample code, there is error message (screenshot attached).

    What go wrong?

    Thanks.

    Tuesday, March 12, 2019 3:12 AM
  • That is telling you that you are in a static method, but fLoginUsername is not static.
    Tuesday, March 12, 2019 3:17 AM
  • How should I write it in order to have global Connecting String?

    Your example is not an optimal approach. It means that every user that uses the program, the database must know about the user-id and password used by the user using the  program, which means more database maintenance to you to establish a new user's user-id and password to access the database.

    As opposed to you using a generic  user-id and password for the database, the generic user-id and password is used by all users using the program to access the database, the generic user-id and password are static on the connectionstring, the user doesn't have to enter the user-id and password and you don't have to have logic in the program if the user-id and password entered by the user are invalid.

    You control the user-id and password, the generic and single usage user-id and password, and not the other way around.

    Tuesday, March 12, 2019 3:30 AM
  • That is telling you that you are in a static method, but fLoginUsername is not static.

    Thank you very much.

    As a newbie, I don't quite understand, but I will do more research on it.

    Another question:

    If I add a new class for SomeGlobalVariables.cs, and ConnectionString will be in the class,  as above code shows, ConnectionString refers to fLoginUsername and fLoginPassword. fLoginUsername and fLoginPassword are from two textbox of child form FormLogin . 

    If user enters correct Username/Password, the program will hide/close FormLogin, and another child form FormChild2 will open. 

    If the program refers to global variable ConnectionString in FormChild2, will ConnectionString picks up fLoginUsername and fLoginPassword ??? Since FormLogin is closed/hided.

    Thanks.

    Tuesday, March 12, 2019 3:36 AM
  • I understand your point, BUT I don't work for IT department, I just build application for team members. I don't know where the User table is and if I have the permission to access the table.

    So forget about other approach requiring IT's permission, it is just bureaucratic. I just use connection string to test opening database, if successfully opens, the pass Username/Password to global variable ConnectionString

    Tuesday, March 12, 2019 3:42 AM
  • I understand your point, BUT I don't work for IT department, I just build application for team members. I don't know where the User table is and if I have the permission to access the table.

    So forget about other approach requiring IT's permission, it is just bureaucratic. I just use connection string to test opening database, if successfully opens, the pass Username/Password to global variable ConnectionString

    So just where are you keeping all these user-id(s) and passwords for the a connectionstring for each user? How do you know if the user-id and password entered is invalid? Is it just one set of user-id and password that all users are entering, and if so that it's just one set of user-id and password, then why does the user have to enter them at all?

    And you have a connectionsstring to what kind of a database? What is the database?

     
    • Edited by DA924x Tuesday, March 12, 2019 3:56 AM
    Tuesday, March 12, 2019 3:54 AM
  • I am not sure exactly what you are asking, but I will try to answer anyway.

    If you set ConnectionString, it will not matter if you then close down the form where you got the value.

    SomeGlobalValues.ConnectionString = Whatever(); // Where 'Whatever' gets the string from the form.  You can close the form after this,

                                                                                     // and ConnectionString will be remembered.

    Tuesday, March 12, 2019 3:59 AM
  • I understand your point, BUT I don't work for IT department, I just build application for team members. I don't know where the User table is and if I have the permission to access the table.

    So forget about other approach requiring IT's permission, it is just bureaucratic. I just use connection string to test opening database, if successfully opens, the pass Username/Password to global variable ConnectionString

    So just where are you keeping all these user-id(s) and passwords for the a connectionstring for each user? How do you know if the user-id and password entered is invalid? Is it just one set of user-id and password that all users are entering, and if so that it's just one set of user-id and password, then why does the user have to enter them at all?

    And you have a connectionsstring to what kind of a database? What is the database?

     

    What do you mean keeping? I would not use the word keeping. I have a child form FormLogin, there are two textbox for Username/Password, with textbook input value, the program will test textbox input value via below code. If connection successfully, then hide FormLogin and switch to another child form, from there, if user runs a query or something, the program will need to use ConnectionString.

    Now I just need to move ConnectionString to class SomeGlobalVariables  

    I am not sure how it works exactly, that is why I am asking here. The bottom line is: if user provides correct Username/Password, then the program should try to figure out how to hold Username/Password for later use, till user closes the program.

    No, each person has his/her own Username/Password, it is the Username/Password for accessing enterprise database, this C# project is also about interacting with the database.

    OdbcConnection Cn; 
    Username = txtUsername.Text;
    Password = txtPassword.Text;
    ConnectionString = "Driver={Adaptive Server Enterprise};server=*****;port=*****;db=*****;uid=" + Username + ";pwd=" + Password + ";";
    
    
    Cn = new OdbcConnection(ConnectionString);
    bool SuccessfulConnection = false;            
    try
    {
    Cn.Open();
    SuccessfulConnection = true;
    }
    catch
    {
    MessageBox.Show("Invalid Username/Password.");
    }
    if (SuccessfulConnection == true)
    {
    //place form code here 
    MessageBox.Show("Login successful.");
    }





    • Edited by VA_er Tuesday, March 12, 2019 4:10 AM
    Tuesday, March 12, 2019 4:02 AM
  • Ok, Thanks. Let me explain it as clearly as I can.

    MainForm is loaded ---> then child form FormLogin opens ----> User enters Username/Password in FormLogin (so at this point, can Global Variable ConnectionString picks up textbox input value Username/Password?) ---> FormLogin closed or hided ----> Another child form FormChild2 opens ---> There is a button in FormChild2 for user to run query from database, which requires using ConnectionString, at this point, can ConnectionString still remember Username/Password (since FormLogin is closed) ? 

    Tuesday, March 12, 2019 4:20 AM
  • I think I understand now.

    Yes it can. But you must remember to prefix ConnectionString with the class name (SomeGlobalVariables) because it is static.

    public class FormLogin : Form
    {
       // Other code goes here.
    
       // Some code to set the connection string.
       void SomeMethod()
       {
          Username = txtUsername.Text;
          Password = txtPassword.Text;
    
          // Remember to put 'SomeGlobalVariables' in front of
          // the static variable.
          SomeGlobalVariables.ConnectionString = "Driver={Adaptive Server Enterprise};server=*****;port=*****;db=*****;uid=" + Username + ";pwd=" + Password + ";";
    
       }
    }
    
    
    public FormChild2 : Form
    {
       // Other code here.
    
       void SomeOtherMethod()
       {
          // We can get the connection string like this.
          // 'SomeGlobalVariables' goes in front again.
          string connection = SomeGlobalVariables.ConnectionString;
       }
    }

    • Marked as answer by VA_er Tuesday, March 12, 2019 1:07 PM
    Tuesday, March 12, 2019 4:29 AM
  • What do you mean keeping? I would not use the word keeping. I have a child form FormLogin, there are two textbox for Username/Password, with textbook input value, the program will test textbox input value via below code. If connection successfully, then hide FormLogin and switch to another child form, from there, if user runs a query or something, the program will need to use ConnectionString.

    --------------------------------------------------------------------------------------

    A database usually has two means of autentication,  that is credentials presented to the database that is going to allow a program to access the database. One way to  present the credentials  to the database is using a user-id and password that is known by the database that is going to allow access to the database. The other means of using credentials is to use Windows authentication, meaning the user doesn't have to give a user-id and password and the database will use the user-id and password a user used to login to a computer using the Windows O/S logged user-id and password to login on the domain as the credentials to access the database. 

    The above is internal logic the database uses,  and it is  its own internal stuff that has nothing to do with some User table. This is database 101 basic stuff that a software developer needs and should  to know in order to program effectively against a database.

    The purpose of the connectionstring is to give the credentials to the database that will allow the program to access the database, and the database is going to use an internal mechanism that has been setup by you to allow the database to be accessed.   

    Do you even know what database you are needing? There are databases that you can control that can be used is a team environment like SQLite, Access, MySQL, MS SQL Server Express that can be used that are free to install and use with you acting as the team database administrator. But you have to know how to administer the database. And you the software developer you have to know how to program against the database effectively.  




    • Edited by DA924x Tuesday, March 12, 2019 4:46 AM
    Tuesday, March 12, 2019 4:39 AM
  • I am not sure how it works exactly, that is why I am asking here. The bottom line is: if user provides correct Username/Password, then the program should try to figure out how to hold Username/Password for later use, till user closes the program.

    The user SHOULD NOT have to enter a user-id and password to allow the program to access the database. The connectionstring should be kept in the app.config  that is part of the program's total solution. And anytime the program needs to get the connectionstring, the ConfigurationManager, gets the connectionstring from the app.config that can be used anywhere and at anytime in the program, eliminating the global variable connectionstring nonsense you have going on here.

    c# global connectionstring (configurationmanager.connectionString)

    https://www.youtube.com/watch?v=hdNDfQXB90E


    Tuesday, March 12, 2019 5:00 AM
  • Thank you very much!
    Tuesday, March 12, 2019 1:08 PM
  • Thanks for bring up the term app.config, as a newbie to Visual Studio, I will look into it.

    However, I am not sure if it works for my case: my Visual Studio is installed in local machine, which the application will be used on Desktop virtualization. That being said, I need to build the application on local desktop, then move the application to Desktop virtualization. 

    Only in Desktop virtualization, the application can reach to ODBC database, I am not sure how to connect to database when working on app.config.

    Tuesday, March 12, 2019 1:37 PM
  • Thanks for bring up the term app.config, as a newbie to Visual Studio, I will look into it.

    However, I am not sure if it works for my case: my Visual Studio is installed in local machine, which the application will be used on Desktop virtualization. That being said, I need to build the application on local desktop, then move the application to Desktop virtualization. 

    Only in Desktop virtualization, the application can reach to ODBC database, I am not sure how to connect to database when working on app.config.

    IMO, what you have considered to be the correct path concerning the access of the connectionstring globally is truly not the optimal approach. 

    https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/

    <copied>

    Executable–hosted app.

    These apps have two configuration files: a source configuration file, which is modified by the developer during development, and an output file that is distributed with the app.

    When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.

    Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app. 

    <end>

    myApp.exe.config.is the runtime config file that .NET looks for to obtain config information, like the connectionstring, that must sit at the same location as the myApp.exe

    Secondly, you seem to have not understood that a database has credentials that must be used for a program to login to the database and use it, which is not unlike using credentials to login to the Windows O/S and use it. 

    Virtulization has nothing to do with how .NET expects to find the location of the runtime config file.

    To be honest, all you have done is brought VBA yippee ki yay wild west cowboy skills to C# .NET. You are still talking ODBC, which is COM,  when you should be talking ADO.NET

    • Marked as answer by VA_er Tuesday, March 12, 2019 3:54 PM
    Tuesday, March 12, 2019 3:30 PM
  • I am not on professional IT career, indeed, I don't know many IT terms.

    The issue is I don't understand your provided youtube link ( https://www.youtube.com/watch?v=hdNDfQXB90E ), at time point 0:31, how can I connect to database to find Connection String. As I said, when I am on Visual Studio, I am on local machine (not on Desktop Virtualization), but the database is on Desktop Virtualization.

    • Edited by VA_er Tuesday, March 12, 2019 4:13 PM
    Tuesday, March 12, 2019 3:55 PM
  • I am not on professional IT career, indeed, I don't know many IT terms.

    The issue is I don't understand your provided youtube link ( https://www.youtube.com/watch?v=hdNDfQXB90E ), at time point 0:31, how can I connect to database to find Connection String. As I said, when I am on Visual Studio, I am on local machine (not on Desktop Virtualization), but the database is on Desktop Virtualization.

    Your problem is you want to put the cart before the horse, becuase you think you have some skills with you working with VBA. I'll say it to you again. VBA and COM are not C# and .NET. ADO.NET is not ODBC. What does Desktop Virtualization have anything to do with knowing the bare basics of how C# and .NET works?

    You don't connect to a database to find the connectionstring  that a .NET program uses. The ADO.NET connectionstring allows the .NET program to find the location of the database, login to the database  with proper credentials when appropriate and use the database.

    http://csharp.net-informations.com/ado.net/csharp-connection-string.htm

    A lot of this can be dealt with by you if you will simply get a beginner's C# .NET book, the horse, and read the book and follow example code. 

    Below is one of the horses, you can pay the $39 monthly subscription fee, take any course  or as many courses as you want and stop the subscription.

    https://www.pluralsight.com/browse/software-development/c-sharp2

    You want to make and bake a cake with no basic knowledge or tool skills  on how to make and bake the cake.

    How about you start with the flour?  

    Wednesday, March 13, 2019 5:01 AM