简体   繁体   中英

Specified cast is not valid in foreach

I am using LinqToSql as ORM. I have a form with the following code:

public partial class frmBarcodeList : Form
{

    private ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["myconn"];
    private DatabaseContext _context; 

    public frmBarcodeList()
        {
            _context = new DatabaseContext(connectionString.ConnectionString);
            InitializeComponent();               
        }    
}

In a button handler click I call GetInfoFromDataBase . This is problem method:

private List<TicketInfo> GetInfoFromDataBase()
        {
            try
            {
                var oids = GetSelectedEvents();

                List<Order> orders = new List<Order>();
                List<TicketInfo> data = new List<TicketInfo>();
                foreach (var oid in oids)
                {
                    orders.AddRange(_context.Orders.Where(o => o.ScheduleId == oid && o.IsPayed.Value).ToList());
                }

                foreach (var order in orders)
                {
                    foreach (var detail in order.OrderDetails) ***line 158**
                    {
                        var checkSum = CalculateChecksum(detail.BarCode);
                        TicketInfo info = new TicketInfo();
                        info.Firstname = order.SiteUser.FirstName;
                        info.LastName = order.SiteUser.LastName;
                        info.Email = order.SiteUser.Email;
                        info.Phone = order.SiteUser.PhoneNumber;
                        info.Barcode = string.Format("{0}{1}", detail.BarCode, checkSum);
                        info.FileName = RemoveInvalidFilePathCharacters(string.Format("{0}_{1}", order.EventSchedule.BaseEvent.Name,
                                                                                      order.EventSchedule.RecurrenceStart), "");
                        data.Add(info);
                    }
                }
                return data;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n" + e.StackTrace);
            }

            return null;
        }

I get error:

---------------------------

---------------------------
Specified cast is not valid.
   at System.Data.SqlClient.SqlBuffer.get_Int32()

   at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)

   at Read_OrderDetail(ObjectMaterializer`1 )

   at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()

   at System.Data.Linq.EntitySet`1.Load()

   at System.Data.Linq.EntitySet`1.GetEnumerator()

   at DT.KazBilet2.BarcodeChecker.frmBarcodeList.GetInfoFromDataBase() in C:\Users\Макс\Documents\Visual Studio 2010\Projects\DT.KazBilet2\branches\NewDesk\DT.KazBilet2.BarcodeChecker\frmBarcodeList.cs:line 158
---------------------------
ОК   
---------------------------

Why I get this error?
Thanks.

Type cast errors inside the SqlClient methods when using Linq2SQL are (almost) always due to model errors.

I'd check that the model is consistent with the database, especially that your int fields in the model (get_Int32 gives the error) correspond to an actual integer field in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM