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.
What you not understand ?
– Pizhev Racing Nov 15 '20 at 13:11