0

I have a problem reading sqlite, ie the data in the table can not be read (but the amount of data read), as shown below: enter image description here

XAML:

<ListBox x:Name="dictionary" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center" >
                <ListBox.ItemTemplate>

                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontSize="20" Text="{Binding Id}" Visibility="Collapsed"/>
                            <TextBlock FontSize="20" Text="{Binding Word}" FontWeight="SemiBold" Margin="30,0,0,0"/>
                            <TextBlock FontSize="20" Text="{Binding Translation}" FontWeight="SemiBold" Margin="30,0,0,0"/>
                        </StackPanel>
                    </DataTemplate>

                </ListBox.ItemTemplate>
            </ListBox>

note: The above XAML I used the word text = "Word" and the translation text = "Translation" to test whether the data on sqlite legible or not. Although the use of binding data remains unreadable

code:

public ObservableCollection<ind_dict> ReadIndo()
        {
            var sqlpath = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, @"Assets\kamus.sqlite");
            using (var dbConn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))
            {
                List<ind_dict> myCollection = dbConn.Table<ind_dict>().ToList<ind_dict>();
                ObservableCollection<ind_dict> indoList = new ObservableCollection<ind_dict>(myCollection);
                dictionary.ItemsSource = indoList;
                return indoList;
            }

        }

ind_dict class:

public class ind_dict
        {
            [SQLite.Net.Attributes.PrimaryKey]
            public string Id { get; set; }
            public string Word { get; set; }
            public string Translation { get; set; }

            public ind_dict(string word, string translation)
            {
                Word = word;
                Translation = translation;
            }
        }

How to solve it?

4
  • Have you got any exception when using the posted code? InstalledLocation is is a read-only location, to deal with SQLite files, you'd better put them in folders like LocalFolder. Commented May 5, 2016 at 9:04
  • No exception is displayed, only the data from sqlite file can not be displayed. Results are displayed as shown above. I'm trying to test using the <TextBlock Text = "Word" /> and <TextBlock Text = "Translation" />. If using <texblock Text = "{Binding Word}" /> and <TextBlock Text = {Binding Translation} />, the data displayed is empty. Commented May 9, 2016 at 1:31
  • I tested with your code in my side. However I encountered 'System.MissingMethodException' and the additional information is Constructor on type '<namespace>.ind_dict' not found.. I add a default constructor in ind_dict class like public ind_dict() { } to fix this issue. After this, your code works well and the data is displayed. Commented May 9, 2016 at 12:30
  • I've added a constructor in ind_dict class, but the data still can not be displayed if using bindings. I include the project:onedrive.live.com/… Please help me solve it. Commented May 10, 2016 at 2:33

1 Answer 1

1

The problem here is that the property in your ind_dict class is not the same as the field name in your kamus.sqlite database.

In your database, the field names are id, word, and translation.
enter image description here

However, in your ind_dict class, the properties are Id, Word, and Translation. When using SQLite.Net-PCL, the properties in classes must match the field names in corresponding tables. Otherwise, the properties can't be populated and will be set to their default value. In your case, your properties' value will be set to null as their type is string. Thus, there will be no data displayed on your page.

To fix your issue, you can change either the field names in your database or the properties in ind_dict class. Here changing the properties in ind_dict class for example:

In ind_dict.cs, change the properties like following:

public class ind_dict
{
    [SQLite.Net.Attributes.PrimaryKey]
    public string id { get; set; }

    public string word { get; set; }
    public string translation { get; set; }

    public ind_dict()
    {
    }

    public ind_dict(string w, string t)
    {
        word = w;
        translation = t;
    }
}

Since we changed the the properties in ind_dict class, we need also change the DataTemplate in XAML:

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <StackPanel Orientation="Horizontal">
                <TextBlock FontSize="20"
                           Text="ID"
                           Visibility="Collapsed" />
                <TextBlock Margin="0,0,0,0"
                           FontSize="20"
                           FontWeight="SemiBold"
                           Text="{Binding word}" />
                <TextBlock Margin="30,0,0,0"
                           FontSize="20"
                           Text="{Binding translation}" />
            </StackPanel>
            <Line Height="5" Stroke="#FFE6E6E6" />
        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>

After this, the data will be displayed in your ListBox.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.