Save data from picker's selected item to firebase realtime database in xamarin form

MSXAM 41 Reputation points
2023-01-02T07:20:45.753+00:00

So I got this problem.
Our other platform( web) insert a data to firebase, data like product. So in our mobile which is I am assign to, I fetch those product data and display to a picker.
So this is How I fetch those data

   //My model  
           public int DeliveryPrice { get; set; }  
           public int PickUp_Price { get; set; }  
           public string gallonType { get; set; }  
           public string refillName { get; set; }  
         
           public int refill_id { get; set; }  
           public string waterType { get; set; }  
     
     
   //function where I fetch data from our database  
     
       public async Task<List<PRODUCT_REFILL>> GetAllProductRefill()  
           {  
               return   
                   (await firebaseClient.Child  
                   (nameof(PRODUCT_REFILL))  
                   .OnceAsync<PRODUCT_REFILL>()).Select(item => new PRODUCT_REFILL  
               {  
                     
                 DeliveryPrice=item.Object.DeliveryPrice,  
                 PickUp_Price=item.Object.PickUp_Price,  
                 gallonType=item.Object.gallonType,  
                 refillName=item.Object.refillName,  
                 waterType=item.Object.waterType,  
                 refill_id=item.Object.refill_id  
         
               }).ToList();  
           }  



   //My xaml  
       <Picker x:Name="Picker_ProductType"                                          
              ItemsSource="{Binding refillName}"  
             ItemDisplayBinding="{Binding refillName}">  
            <Picker.Items  >                                                         
                                               <x:String >  
                                               </x:String>   
                                                <x:String>  
                                               </x:String>  
              </Picker.Items>  
       </Picker>  
     
     
   //xaml.cs code behind  
       Product_RefillRepo productrefill = new Product_RefillRepo();  
         
       async protected override void OnAppearing()  
           {  
              var productRefill = await productrefill.GetAllProductRefill();  
               Picker_ProductType.ItemsSource = productRefill;  
         
            }  

Then in my code behind also I have an button_clicked event and inside that, I have this line.
string orderProductType = Picker_ProductType.SelectedItem.ToString();

but I when I click the order button, I got this error.
Error says; System.InvalidCastException: 'Specified cast is not valid.'

Please help me if how to fix that error cause I cant insert data to firebase realtime. Thank you so much.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-01-03T03:11:38.14+00:00

    Hello,

    When you use Picker_ProductType.SelectedItem, you get the PRODUCT_REFILL object, if you want to the value of refillName, you need to cast Picker_ProductType.SelectedItem to PRODUCT_REFILL, then you can get the refillName like following code.

       private void OnBtnClicked(object sender, EventArgs e)  
           {  
               PRODUCT_REFILL pRODUCT_REFILL = Picker_ProductType.SelectedItem as PRODUCT_REFILL;  
               string orderProductType=pRODUCT_REFILL.refillName;  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.