0

Recently I've been messing around a lot with WPF, ik syntax changes a bit, but some of the problems are probably new.

I am trying to save the connection info in variables in mainform and get the values from within a class, but when I do they are NULL and I don't know why they show up as null (I am even obligated to make them static in class).

public static MainMenu x = new MainMenu();
public string cn = "Data Source=" + x.sqlip + "; Initial Catalog=" + db + "; User ID=" + x.sqlid + "; Password=" + x.sqlpw + ";";

So this is my form call + connection string builder but when i try to get the values they are null.

public string sqlid, sqlpw, sqlip;
//update details
open.sqlid = sqlusr.Text;
open.sqlpw = sqlpwd.Text;
open.sqlip = sqlip.Text;

So in top there are 2 examples. The variable declarations are in the mainmenu form, and the update details are in the loading form when they are connecting.

Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87
Enigma
  • 23
  • 1
  • 10
  • What is open in open.sqlid ? – Muhammad Saqlain Feb 24 '17 at 21:55
  • open = my MainMenu form ( i used open as the name) like i did in x and sqlid,sqlpw and sqlip are the variables to save values for use in my class but it's not working – Enigma Feb 24 '17 at 21:59
  • Do you mean that `sqlid`, `sqlpw`, and `sqlip` are all null? Because you're never setting them to anything. `open.sqlid` is not the same as `sqlid`. – Broots Waymb Feb 24 '17 at 22:03
  • You need to show more complete code. It is not clear what is happening from what you have here. All I can say is that in your second block of code, sqlid, sqlpw, and sqlip will all definitely be null. – Broots Waymb Feb 24 '17 at 22:07
  • Alright lets go by steps. 1º connect and save values to my next form named MainMenu . 2º In mainmenu i got that 3 variables , sqlid,pw and ip and what i do in open.sqlid = sqlusr.Text is send value from textbox sqlusr for my variable id in MainMenu. 3º I got a class named getInfo where i call my functions to execture querrys but i got a public static connection string right there declared and i public static MainMenu too for can acess the values in mainmenu ( the 3 variables) but when i do, they send me null values – Enigma Feb 24 '17 at 22:08

1 Answers1

0

Created an example. Try this hope you understand the role of static

class Program
    {
       public static class MainMenu
        {
            public static string sqlid = null;
            public static string sqlpw = null;
        }

//Now a function will update values of variables

        public void updateDetails()
        {
            MainMenu.sqlid = sqlusr.Text;
            MainMenu.sqlpw = sqlpwd.Text;
        }

//Now if i need to use variable values

        static void Main(string[] args)
        {
            Program p = new Program();
            p.updateDetails();

            public string cn = "Initial Catalog=" + db + "; User ID=" + MainMenu.sqlid + "; Password=" + MainMenu.sqlpw + ";";

        }
}
Muhammad Saqlain
  • 2,112
  • 4
  • 33
  • 48