How to display data in conrols when a form loads

some1some1 Member ✭✭✭
edited May 7 in Xamarin.Android

I have saved datas in COMPANY_MASTER tabe in sqlite where id =1 and when i load that form again i want that data to be displayed in the controls.

Xaml code

` <ContentPage.ToolbarItems>

    <ToolbarItem  Text="Edit" Activated="OnEdit"  Order="Secondary"/>

    <ToolbarItem  Text="Save" Activated="OnSave"  Order="Primary" />

</ContentPage.ToolbarItems>

<ContentView>

    <ScrollView>

        <StackLayout>

            <Grid x:Name="gridLayout" HorizontalOptions="FillAndExpand">

                <Grid.RowDefinitions>

                    <RowDefinition Height="50"/>


            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

                    <RowDefinition Height="50"/>

                    <RowDefinition Height="50"/>

                    <RowDefinition Height="50"/>

                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="130"/>

                    <ColumnDefinition Width="2*"/>

                </Grid.ColumnDefinitions>

                <Label Text="Company Name" Grid.Row="0" Grid.Column="0"/>

                <Entry x:Name="ECompnyName"  HorizontalOptions="FillAndExpand" Grid.Row="0" Grid.Column="1" IsEnabled="False" Text="{Binding Name}"/>

                <Label Text="Company Address" Grid.Row="1" Grid.Column="0"/>

                <Entry x:Name="ECompnyAdd1"  HorizontalOptions="FillAndExpand" Grid.Row="1" Grid.Column="1" IsEnabled="False" Text="{Binding Address1}"/>

                <Entry x:Name="ECompnyAdd2"  HorizontalOptions="FillAndExpand" Grid.Row="2" Grid.Column="1" IsEnabled="False" Text="{Binding Address2}"/>

                <Entry x:Name="ECompnyAdd3"  HorizontalOptions="FillAndExpand" Grid.Row="3" Grid.Column="1" IsEnabled="False" Text="{Binding Address3}"/>

                <Label Text="``Company Email Id" Grid.Row="4" Grid.Column="0"/>

                <Entry x:Name="ECompnyEmail"  HorizontalOptions="FillAndExpand" Grid.Row="4" Grid.Column="1" IsEnabled="False" Text="{Binding Email}"/>

                <Label Text="Company Phone" Grid.Row="5" Grid.Column="0"/>

                <Entry x:Name="ECompnyPhone"  HorizontalOptions="FillAndExpand" Grid.Row="5" Grid.Column="1" IsEnabled="False" Text="{Binding PhoneNo}"/>

                <Label Text="Company Type" Grid.Row="6" Grid.Column="0" Margin="3" />

                <Picker x:Name="pickCompnyType"  Grid.Row="6" Grid.Column="1" IsEnabled="False"  >

                    <Picker.Items>

                        <x:String>Academic</x:String>

                        <x:String>Financial</x:String>

                    </Picker.Items>

                </Picker>

                <Label Text="Wing" Grid.Row="7" Grid.Column="0"/>

                <Entry x:Name="EWing"  HorizontalOptions="FillAndExpand" Grid.Row="7" Grid.Column="1" IsEnabled="False" Text="{Binding Wing}"/>

                <Label Text="Year" Grid.Row="8" Grid.Column="0" Margin="3" />

                <Picker x:Name="pickYear"  Grid.Row="8" Grid.Column="1" IsEnabled="False"  >

                    <Picker.Items>

                        <x:String>April - March</x:String>

                        <x:String>January - December</x:String>

                    </Picker.Items>

                </Picker>

            </Grid>

        </StackLayout>

    </ScrollView>

</ContentView> `

xaml.cs

` private string type;

    private string year;

    private SQLiteAsyncConnection _connection;

    public frmSettings ()

    {

        InitializeComponent ();

        _connection = DependencyService.Get<ISQLiteDb>().GetConnection();

    }

    protected async override void OnAppearing()

    {

       await _connection.CreateTableAsync<COMPANY_MASTER>();

        base.OnAppearing();

    }

    private void OnEdit(object sender, EventArgs e)

    {

        ECompnyName.IsEnabled = true;

        ECompnyAdd1.IsEnabled = true;

        ECompnyAdd2.IsEnabled = true;

        ECompnyAdd3.IsEnabled = true;

        ECompnyEmail.IsEnabled = true;

        ECompnyPhone.IsEnabled = true;

        pickCompnyType.IsEnabled = true;

        pickYear.IsEnabled = true;

        EWing.IsEnabled = true;

    }

    private async void OnSave(object sender, EventArgs e)

    {

        try

        {

            if (pickCompnyType.SelectedIndex == 0)

            {

                type = "Academic";

            }

            else if(pickCompnyType.SelectedIndex == 1)

            {

                type = "Financial";

            }

            if (pickYear.SelectedIndex == 0)

            {

                year =" April - March";

            }

            else if(pickYear.SelectedIndex==1)

            {

                year = "January - December";

            }

            var cmpny = new COMPANY_MASTER

            {

                Name = ECompnyName.Text,

                PhoneNo = ECompnyPhone.Text,

                Email = ECompnyEmail.Text,

                Address1 = ECompnyAdd1.Text,

                Address2 = ECompnyAdd2.Text,

                Address3 = ECompnyAdd3.Text,

                Type = type,

                Wing = EWing.Text,

                Year =year

            };
            var checker = await _connection.InsertAsync(cmpny);

            if (checker == 1)

            {

                await DisplayAlert("Message", "Data saved successfully.", "Ok")

                await Navigation.PopAsync();
            }

        }
        catch

        {

            await DisplayAlert("Error", "Error in data saving. Please retry later.", "Ok");
        }
    }`

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited May 8

    Bind the data from sqlite to the controls.
    Check the Xamarin.Forms MVVM: SQLite Sample for Beginners.

  • some1some1 Member ✭✭✭

    @yelinzh this link is not available

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited May 8

    @DoraBhuji
    Sorry for my mistake, I've modified it.

  • some1some1 Member ✭✭✭

    @yelinzh thankyou .
    i dont have listview. mrequirement is that through form i will save data corresponding to id =1.and when i load the form again i want the saved data displayed in controls corresponding to id=1.how will i do that.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Listview is just an example that you can refer to. Read the data through the "id" in the database and set the value to the control by binding the data.

  • some1some1 Member ✭✭✭
    edited May 9

    @yelinzh this error is occuring when writing the function

  • JarvanJarvan Member, Xamarin Team Xamurai

    Are these two Classes the same type?

  • some1some1 Member ✭✭✭
    edited May 9

    @yelinzh thank you for replying . i have solved that error. i got it.

    actually i want to update COMPANY_MASTER's opening_cash and available cash from another form . how will i do that.

    all other fields are saved from this frmSettings.xaml form.
    How will i write that update function.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Do you mean updating the data of a control when another table is changed. If so, you can detect the table's updating. After updating the table, COMPANY_MASTER will also be updated.

Sign In or Register to comment.