简体   繁体   中英

Variables C# UWP

I have two files. Need to get variables from one to another. I set them as global in the file where they are defined and filled. But in the second file when it reads variables, they are empty. How can I solve this problem? The code from the first file:

public sealed partial class NewOrder : Page
{
    public string Name1;
    public string Mob;
    public string Adres;
    public string Email;
    public string telephone; 


    public NewOrder()
    {
        searchButton.Click += async delegate
 { 
        telephone = searchtext.Text;

        using (MySqlConnection connection = new MySqlConnection("...."))
        {
            connection.Open();
            MySqlCommand createCommand = new MySqlCommand(
                "SELECT * FROM customers WHERE mob LIKE N'" + telephone + "'", connection);

            EncodingProvider ppp;
            ppp = CodePagesEncodingProvider.Instance;
            System.Text.Encoding.RegisterProvider(ppp);
            MySqlDataReader reader = createCommand.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Name1 = reader.GetString(1);
                    Mob = reader.GetString(2);
                    Adres = reader.GetString(3);
                    Email = reader.GetString(4);
                }
            }
        }
    }
}
}

The code from the second file:

public sealed partial class Details : Page
{         
    public Details()
    {
        this.InitializeComponent();

        NewOrder A = new NewOrder();
        name.Text = A.Name1;
        tel.Text = A.Mob;
        adres.Text = A.Adres;
        email.Text = A.Email;                       
    }                   
}

That what I see.
The exception is here: name.Text = A.Name1;

An exception of type 'System.ArgumentNullException' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: Value cannot be null.

replace your code with this,

    public string Name1 = string.Empty;
    public string Mob = string.Empty;
    public string Adres = string.Empty;
    public string Email = string.Empty;
    public string telephone = string.Empty;

Besides allot of bad form, reading sql in a constructor, not initializing your unit variables in the constructor, not using a separate method to read the db which somehow lets you know if the read was successful, for each row in the set you would be overwriting your class variables (should be a list) and no exception handling...

The reason appears to be you did not read any data into your variables so they would have the possibility of being null. If this is your intended design then you should null check because you may not have data or some other issues caused reader.HasRows to be false (like you threw an exception trying to read the db).

if (A.Name1 != null)
{
   name.Text = A.Name1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM