Click here to Skip to main content
15,846,977 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have simple Xamarin code to bind data from online db.
Labels are shown.
Between labels are empty.
No data.
Database connection is ok.
Some help?

What I have tried:

MainPage XML CS
C#
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Xamarin.Forms;

namespace myApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            GetDataFromDatabase();
        }

        async void GetDataFromDatabase()
        {
            string connectionString = 
                           Server=yourserver;Database=yourdatabase;
                                      Uid=yourusername;Pwd=yourpassword;";

            using (MySqlConnection connection = 
                   new MySqlConnection(connectionString))
            {
                try
                {
                    await connection.OpenAsync();

                    string query = "SELECT YourColumnName FROM YourTable";
                    using (MySqlCommand cmd = 
                           new MySqlCommand(query, connection))
                    using (MySqlDataReader reader = cmd.ExecuteReader())
                    {
                        List<YourDataModel> data = 
                                new List<YourDataModel>();

                        while (await reader.ReadAsync())
                        {
                            data.Add(new YourDataModel
                            {
                                YourColumnName = 
                                    reader["YourColumnName"].ToString()
                            });
                        }

                        dataListView.ItemsSource = data;
                    }
                }
                catch (Exception ex)
                {
                    // Handle any exceptions
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }

    public class YourDataModel
    {
        public string YourColumnName { get; set; }
    }
}

And add to XML to show data in app:
XML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App10.MainPage">

    <StackLayout>
        <Label Text="Welcome" TextColor="Black"/>
        <ListView x:Name="dataListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding YourColumnName}" 
                TextColor="Black"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <Label Text="Work label after table binding" 
        TextColor="Blue" />
    </StackLayout>

</ContentPage>
Posted
Updated 5-Nov-23 1:17am
v2
Comments
Richard MacCutchan 30-Oct-23 7:02am    
Use the debugger to see how many (if any) records are added to your list.
Stylus STYLUS 30-Oct-23 7:37am    
I check now. Table hae 3 records
Richard MacCutchan 30-Oct-23 7:51am    
Well that does not prove that your code is reading them.
Stylus STYLUS 30-Oct-23 8:02am    
I try to solve that :-)
Dave Kreskowiak 30-Oct-23 10:04am    
If that's your actual code, you should be getting error messages all over the place. Your exception handling code is dumping those errors to Console instead of Debug or Trace.

Comment out the try/catch lines and run it again. Now you should get actual error messages when you run it.

Also, it looks like you copied this code from somewhere and seriously forget to modify it to your database and needs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900