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

  • Question

  • User398771 posted

    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:

    ```

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

    ```

    Thank's.

    Saturday, January 9, 2021 12:25 PM

Answers

  • User371688 posted

    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}"/>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, January 14, 2021 9:49 AM

All replies

  • User371688 posted

    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.

    Monday, January 11, 2021 7:09 AM
  • User398771 posted

    @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.

    Monday, January 11, 2021 4:05 PM
  • User371688 posted

    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

    ```

            <!--save all entry-->
            <Button Text="Save" Command="{Binding SaveCommand}"/>
    
            <!--clear all entry-->
            <Button Text="Clear" Command="{Binding ClearCommand}"/> 
        </StackLayout>
    </ContentPage.Content>
    

    ```

    Tuesday, January 12, 2021 8:40 AM
  • User398771 posted

    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?

    Tuesday, January 12, 2021 3:26 PM
  • User371688 posted

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

    Wednesday, January 13, 2021 2:14 AM
  • User398771 posted

    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

    Wednesday, January 13, 2021 11:40 AM
  • User371688 posted

    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}"/>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, January 14, 2021 9:49 AM
  • User398771 posted

    I try and it works. Thanks @jezh

    Sunday, January 17, 2021 12:44 AM