C# Firebase System.InvalidCastException in Dictionary

Viewed 38

I am new to C#. I'm trying to build a demo application on airline management. I uploaded data to firebase. when I tried to retrieve data it's showing System.InvalidCastException: 'Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.'

how can i prevent it?

my class:

class AddFlightNew_class
    {
        public string Fcode { get; set; }
        public string Source { get; set; }
        public string Destination { get; set; }
        public string TicketPrice { get; set; }
    }

uploading data to Firebase code:

private void AddBtn_Click(object sender, EventArgs e)
        {
            if (FcodeTb.Text == "" || SourceCb.SelectedItem.ToString() == "" || DestinationCb.SelectedItem.ToString() == "" || TicketPriceTb.Text == "")
            {
                MessageBox.Show("Missing Information");
            }
            else if (SourceCb.SelectedItem.ToString() == DestinationCb.SelectedItem.ToString())
            {
                MessageBox.Show("Source and Destination can't be same");
            }       
            else
            {
                try
                {


                    AddFlightNew_class adp = new AddFlightNew_class()
                    {
                        Fcode = FcodeTb.Text,                                  //textbox
                        Source = SourceCb.SelectedItem.ToString(),             //combobox
                        Destination = DestinationCb.SelectedItem.ToString(),    //combobox
                        TicketPrice = TicketPriceTb.Text                         //textbox
                    };

                    //Firebase upload
                    FirebaseResponse response = client.Set("AddFlightNew/" + FcodeTb.Text, adp);
                    MessageBox.Show("Save Successful");
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message);
                }

                populate();  //for retrieving 
            }
        }

retrieving data and the error:

private void populate()
        {
            
            AddFlightDGVnew.DataSource = null;           //DataGridView
            AddFlightDGVnew.Rows.Clear();

            FirebaseResponse response = client.Get("AddFlightNew/");
           
            Dictionary<string, AddFlightNew_class> Flight = response.ResultAs<Dictionary<string, AddFlightNew_class>>();     // **here is the error: System.InvalidCastException: 'Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.'**



            
            foreach (var get in Flight)
            {


                    //adding rows to DataGridView
                AddFlightDGVnew.Rows.Add(   
                                          
                    get.Value.Fcode,
                    get.Value.Source,
                    get.Value.Destination,
                    get.Value.TicketPrice
                   

                    );
            }


        }

can anyone help me to solve this?

0 Answers
Related