Home > OS >  Picker item value insert to database(firebase) in xamarin form
Picker item value insert to database(firebase) in xamarin form

Time:12-01

My problem is to insert data to firebase in xamarin form. This is my CreateAccount.xaml `

<Picker 
                                Title="Select Security Question" 
                                TextColor="Black"
                                HorizontalOptions="StartAndExpand"
                                FontSize="Medium"
                                WidthRequest="250"
                                TitleColor="Black"
                                x:Name="entryField_SecurityQuestion"
                                >
                                <Picker.Items 
                                
                                    >
                                
                                    <x:String>
                                        Who is your first love?
                                    </x:String>
                                    <x:String>
                                       what subject you failed?
                                    </x:String>
                                     <x:String>
                                       What is the name of your dog?
                                    </x:String>
                                     <x:String>
                                       Who is your first kiss?
                                    </x:String>
                                    
                                </Picker.Items>
                                
                            </Picker>

codebehind.cs

async public void signButton_Clicked(System.Object sender, System.EventArgs e)
        {

           Picker picker = sender as Picker;

            //this is how I manage my some ID from my front end
            string firstName = entryField_Firstname.Text;
            string lastName = entryField_Lastname.Text;
            string contactNumber = entryField_PhoneNumber.Text;


             string securityQuestion = picker.SelectedItem.ToString(); // this line is my trial and error, I tried some code but I cant insert data to database

            Customer customer = new Customer();
            customer.FirstName = firstName;
            customer.LastName = lastName;
            customer.SecurityQuestion = securityQuestion;//this one also, this is my trial and error approach 
            customer.ContactNumber = contactNumber;

            var Savedata = await customerRepo.Save(customer);
         }

`

I expect that, I can get the picker's item, and save it to database. Any link that will be posted related to my post, will be appreciated. Thank you so much

CodePudding user response:

If you want to get the Picker's SelectedItem, you need to refer to the x:Name="entryField_SecurityQuestion" of your picker not Picker picker = sender as Picker;,just as Jason said.

You can refer to the following code:

        private void signButton_Clicked(object sender, EventArgs e) 
    { 
       // remove the following code
        //Picker picker = sender as Picker;
         
        string securityQuestion = entryField_SecurityQuestion.SelectedItem.ToString();

        Customer customer = new Customer();
        customer.FirstName = firstName;
        customer.LastName = lastName;
        customer.SecurityQuestion = securityQuestion;

    }
  • Related