0

I try to display some with places from database on the SearchBar with xamarin.

I use this method with button and I not have any problems.. everything is ok..

void Btn_GetPlaces(System.Object sender, System.EventArgs e)
    {
        try
        {
            connection.Open();
            LabelSQL.Text = "Global connection to database is open.";

            var cmd = new MySqlCommand();
            cmd.Connection = connection;

            MySqlCommand command = new MySqlCommand($"CALL nearest3({GlobalLat}, {GlobalLong}, 1)", connection);

            using (MySqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    // access your record colums by using reader
                    LabelPlace.Text = (reader[0]).ToString();
                }
            }
            
        }
        
        catch (Exception ex)
        {
            LabelSQL.Text = (ex.ToString());
        }
        connection.Close();
    }

When I want to use another select query to put my staff on the for SearchBar I have a connection problem on this part of code:

IEnumerable<Contacts> GetContacts(string searchText = null)
    {
        connection.Open();
        var cmd = new MySqlCommand();
        cmd.Connection = connection;
        var contacts = new List<Contacts>();
        MySqlCommand command = new MySqlCommand($"SELECT place,x,y FROM places ORDER BY place", connection);
        
        using (MySqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                var place = new Contacts
                {
                    Place = reader[0].ToString()
                };
                contacts.Add(place);  
            }
            connection.Close();
        }

        if (string.IsNullOrEmpty(searchText))
            return contacts;
        return contacts.Where(p => p.Place.StartsWith(searchText));
    }

Here in the constructor I set the connection string with this code:

public MainPage()
    {
        InitializeComponent();

        listView.ItemsSource = GetContacts();
        this.BindingContext = this;

        server = "192.168.0.1,3306";
        database = "NameDB";
        uid = "userr";
        password = "pass@";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        connection = new MySqlConnection(connectionString);
    }

Is there a way to fix this problem ? Can I get some example, because Im new to this world. Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pizhev Racing
  • 466
  • 1
  • 6
  • 23
  • 1
    What problem do you need to fix? Your question does not contain any description of an actual problem. Your title mentions a NullRef exception, but there is not detailed description of this in your post. Which **specific** line causes the exception, and what is the stack trace? – Jason Nov 15 '20 at 13:05
  • I say: When I want to use another select querry to put my staff on the for SearchBar I have a connection problem on this part of code: What you not understand ? – Pizhev Racing Nov 15 '20 at 13:11
  • Again, which **specific** line causes the exception? – Jason Nov 15 '20 at 13:20
  • The error is on: connection.Open(); – Pizhev Racing Nov 15 '20 at 13:22
  • When it stands to reason that your `connection` object is null. Test for this and create a new one if necessary. – Jason Nov 15 '20 at 13:25

0 Answers0