In web applications, you can make a variable global by using the app state of the application. You can also declare variable in a session for the session level. But in a WinForms application, you can't use app or session states. However, you can make a global variable by creating a static class in a separate class file in your application.
First, create a class called global in your application with the code given below. The class also has a static variable.
- class Global
- {
- public static string UserID;
- }
- Global.UserID = comboBox1.Text.ToString();
- if(Global.UserID == "abdulateef")
- {
- dataGridView1.Refresh();
- string ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};Server=localhost;Database="";User Id="";Password=";
- string query = "Select filename, encfile, password,date,username FROM encinfor";
- OdbcConnection conn = new OdbcConnection(ConnectionString);
- conn.Open();
- try
- {
- //populating the gridview1 on form load
- DataSet ds = new DataSet();
- using(OdbcDataAdapter adapater = new OdbcDataAdapter(query, conn))
- {
- adapater.Fill(ds);
- dataGridView1.DataSource = ds.Tables[0];
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- class Global
- {
- public static string UserID;
- public static string Status;
- public static string Adim;
- }
C# now supports static classes. Learn more here: Static Class In C#
Learn more about Session and Application States in ASP.NET.

Ravi KumarPosted Aug 9, 2019, 1:07 AM
Where should I create global class
SHIVSHANKER HANDEPosted Feb 15, 2017, 9:30 PM
Global is not a static class ........ without creating object of global class is it possible to access the global class member