0

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.

  • Put a breakpoint in Name1 = reader.GetString(1); and inspect the value of Name1. – mattinsalto Oct 04 '16 at 08:17
  • I expect your query to not return rows. Are you sure that whatever you type in searchText has actually a row in table customers that matches the value in mob (or if you use wildcards multiple rows). – rene Oct 04 '16 at 08:21
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – rene Oct 04 '16 at 08:23
  • 1
    After the constructor completed there will be no values in `Name1` a.s.o. - they are filled, if at all, after clicking the search button. Don't know if assigning null to `Text` in UWP causes the `ArgumentNullException`, but this appears to be the only reason from code – JeffRSon Oct 04 '16 at 08:27

2 Answers2

0

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;
Maulik Shah
  • 673
  • 7
  • 19
  • This is more a fix for the symptom but I doubt it is the root cause – rene Oct 04 '16 at 08:22
  • it may be, but I think so it is better way to avoid 'System.ArgumentNullException' initialize value to object. Give default value to variable or object of class with like someobj = new MyClass(). – Maulik Shah Oct 04 '16 at 08:27
  • There is a chance that query not return any record. If query not return any rows then initialization avoid null and return default value. So in my case suppose there is no record found then name.Text = A.Name1; will be something like name.Text = string.Empty; so the text property of name control will be blank no the null. – Maulik Shah Oct 04 '16 at 08:31
0

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;
}
Mark
  • 39
  • 7