Forum Xamarin.Forms

How to connect specific database if there are 2 or more data models in one database xamarin?

ycusoyycusoy Member ✭✭

previously I have created a data model as below. I have successfully loaded the data into a listView for the public class student.

Then, how do you connect data from a public class school that is only input once without using a listView and PrimaryKey?

using SQLite;
using System;

namespace SchoolData.Models
{
    public class Student
    {   
        [PrimaryKey, AutoIncrement]
        public int IdSis { get; set; }
        public DateTime Date { get; set; }
        public string PicStudent { get; set; }
    }

    public class School
    {
        public string SchoolName { get; set; } //without primary key or single entry
        public string Address { get; set; }
        public string Vision { get; set; }
    }
}

XamlPage code overview:

<StackLayout>
    <Label Text="School Name:"/>
    <Entry Text="{Binding SchoolName}"/>
    <Label Text="Address:"/>
    <Entry Text="{Binding Address}"/>
    <Label Text="Vision:"/>
    <Entry Text="{Binding Vision}"/>

    <Button Text="Save" Clicked="SaveClick"/> //save all entry
    <Button Text="Clear" Clicked="ClearClick"/> //clear all entry
</StackLayout>

Thank's.

Best Answer

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    how I will save the public class schol data from SchoolData.Models

    When we change the values in the Entries by the following code, the fields of the school(SchoolName,Address and Vision) change accordingly.

    That's what the code Mode=TwoWay does.

            <Label Text="School Name:"/>
            <Entry Text="{Binding school.SchoolName,Mode=TwoWay} "/>
            <Label Text="Address:"/>
            <Entry Text="{Binding school.Address,Mode=TwoWay}"/>
            <Label Text="Vision:"/>
            <Entry Text="{Binding school.Vision,Mode=TwoWay}"/>
    

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Then, how do you connect data from a public class school that is only input once without using a listView and PrimaryKey?

    I don't quite understand what you mean, you can't get the data or something else? Can you elaborate on this?


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • ycusoyycusoy Member ✭✭

    @jezh said:

    sorry, this is because my english is not good. I mean, how to output the contents of public class scholl into Labels. not into the ListView.

  • jezhjezh Member, Xamarin Team Xamurai
    edited January 13

    You can just create a viewmodel( SchoolViewModel) for your page.

    You can refer to the following code:

    SchoolViewModel.cs

       public  class SchoolViewModel
        {
            public School school { get; set; }
    
            public ICommand SaveCommand { get; set; }
    
            public ICommand ClearCommand { get; set; }
    
            public SchoolViewModel()
            {
                this.school = new School { SchoolName  ="school1", Address = "school1 address", Vision= "Vision1" };
    
    
                SaveCommand = new Command(() => {
                    Debug.WriteLine("Save button is clicked. Name = " + school.SchoolName);
    
                    Debug.WriteLine("Saved School Name is:  " + school.SchoolName);
                });
    
                ClearCommand = new Command(() => {
                    Debug.WriteLine("Clear button is clicked.  " );
    
                    school.SchoolName = "";
    
                    System.Diagnostics.Debug.WriteLine(" School Name is:  " + school.SchoolName);
                });
            }
        }
    

    StudentPage.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:school="clr-namespace:SwitchDemo.school"
                 x:Class="SwitchDemo.StudentPage"
                  x:Name="page"
                 >
        <ContentPage.BindingContext>
            <school:SchoolViewModel></school:SchoolViewModel>
        </ContentPage.BindingContext>
        <ContentPage.Content>
            <StackLayout>
                <Label Text="School Name:"/>
                <Entry Text="{Binding school.SchoolName,Mode=TwoWay} "/>
                <Label Text="Address:"/>
                <Entry Text="{Binding school.Address,Mode=TwoWay}"/>
                <Label Text="Vision:"/>
                <Entry Text="{Binding school.Vision,Mode=TwoWay}"/>
    
    
                <!--save all entry-->
                <Button Text="Save" Command="{Binding SaveCommand}"/>
    
                <!--clear all entry-->
                <Button Text="Clear" Command="{Binding ClearCommand}"/> 
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    
  • ycusoyycusoy Member ✭✭

    all went well when executed. then I realized, it turns out that the save button and the command are not there ... :)
    how to save it by clicking on the save button?

  • jezhjezh Member, Xamarin Team Xamurai

    Hi @ycusoy , I have updated my answer, you can check it. Hope it can help you. :)

  • ycusoyycusoy Member ✭✭

    I've tried it again and it looks like I have to get back to the topic again. I use sqlite to store data. how I will save the public class schol data from SchoolData.Models, while the command to save it is limited to the console

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    how I will save the public class schol data from SchoolData.Models

    When we change the values in the Entries by the following code, the fields of the school(SchoolName,Address and Vision) change accordingly.

    That's what the code Mode=TwoWay does.

            <Label Text="School Name:"/>
            <Entry Text="{Binding school.SchoolName,Mode=TwoWay} "/>
            <Label Text="Address:"/>
            <Entry Text="{Binding school.Address,Mode=TwoWay}"/>
            <Label Text="Vision:"/>
            <Entry Text="{Binding school.Vision,Mode=TwoWay}"/>
    
  • ycusoyycusoy Member ✭✭
    edited January 17

    I try and it works. Thanks @jezh

Sign In or Register to comment.