简体   繁体   English

为什么从 sqlite db 中选择会显示错误“指定的强制转换无效”。 在 Xamarin 表单中?

[英]Why does select from sqlite db shows error 'Specified cast is not valid.' in Xamarin Forms?

Getting a single value from by SQLite database but I get the error System.InvalidCastException: 'Specified cast is not valid.'从 SQLite 数据库获取单个值,但出现错误 System.InvalidCastException: 'Specified cast is not valid.' I need only the first value of the select results, tried both First() and ElementAtOrDefault(0) but same thing happens.我只需要选择结果的第一个值,尝试了 First() 和 ElementAtOrDefault(0) 但同样的事情发生了。

Error gets catched when trying to assing to the variable IDPromoMainPage, not before.尝试分配给变量 IDPromoMainPage 时捕获错误,而不是之前。

Basically this is how I call the value:基本上这就是我调用值的方式:

private void GetPromoLocal()
        {
            try
            {
                var databasePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "LocalDB.db3");
                var db = new SQLiteConnection(databasePath);
                IEnumerable<T_Promo> resultado = SELECT_WHERE(db);
                if (resultado.Count() > 0)
                {
                    IDPromoMainPage = Convert.ToInt32(resultado.First()); // ElementAtOrDefault(0));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static IEnumerable<T_Promo> SELECT_WHERE(SQLiteConnection db)
        {
            return db.Query<T_Promo>("SELECT IDPromo FROM T_Promo");
        }

Also have the same error happening here, exactly at line "PisterosLista = resultado.Cast().ToList();"这里也发生了同样的错误,正好在“PisterosLista = resultado.Cast().ToList();”行

List<Pisteros> PisterosLista = new List<Pisteros>();

public void GetPisterosLocal()
        {
            try
            {
                var databasePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "LocalDB.db3");
                var db = new SQLiteConnection(databasePath);
                IEnumerable<T_Pisteros> resultado = SELECT_WHERE_Pist(db);
                if (resultado.Count() > 0)
                {
                    PisterosLista = resultado.Cast<Pisteros>().ToList();
                    //Console.WriteLine("content :: " + content);
                    Console.WriteLine("Data :: " + PisterosLista);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static IEnumerable<T_Pisteros> SELECT_WHERE_Pist(SQLiteConnection db)
        {
            return db.Query<T_Pisteros>("SELECT * FROM T_Pisteros");
        }

if the column IDPromo is an int, then just do this如果列IDPromo是一个整数,那么就这样做

    IEnumerable<int> resultado = SELECT_WHERE(db);

    public static IEnumerable<int> SELECT_WHERE(SQLiteConnection db)
    {
        return db.Query<int>("SELECT IDPromo FROM T_Promo");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin 形式:System.InvalidCastException:“指定的强制转换无效。” - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid.' 单击 Pin 时尝试从 API 加载数据 (CustomMap) 错误:指定的转换无效。 - xamarin.forms.maps - Trying to load data from API when Pin is clicked (CustomMap) error: Specified cast is not valid. - xamarin.forms.maps 在这种情况下,为什么“指定的演员表无效”是没有意义的。 或者是吗? - It makes no sense why “Specified cast is not valid.” in this case. Or does it? Xamarin 表单与 Firebase。 数据检索抛出 System.InvalidCastException: &#39;指定的转换无效。&#39; - Xamarin Forms with Firebase. Data retrival throwing System.InvalidCastException: 'Specified cast is not valid.' 自动映射器显示错误“指定的转换无效。” - automapper showing error “Specified cast is not valid.” 如何解决“指定的演员表无效”。 错误 - How to resolve the “Specified cast is not valid.” error 指定的演员表无效 - Xamarin Forms - Specified cast is not valid - Xamarin Forms 指定的强制转换无效(xamarin 形式) - Specified cast is not valid (xamarin forms) 指定的转换在 Xamarin Forms 中无效 - Specified cast is not valid in Xamarin Forms 指定的演员表无效。 Windows Server 2008 R2中显示错误 - Specified cast is not valid. error shows in window server 2008 r2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM