简体   繁体   English

指定的演员表无效。 林克

[英]specified cast is not valid. linq

While inserting the data from excel to database it throwing the above error 将数据从Excel插入数据库时​​,会引发上述错误

Here is my code: 这是我的代码:

void insertDB()
       {
           string FileName = lblFileName.Text;
           string Extension = Path.GetExtension(FileName);
           string FolderPath = Server.MapPath(ConfigurationManager.AppSettings["FolderPath"]);
           string conStr = "";
           switch (Extension)
           {
               case ".xls": //Excel 97-03
                   conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FolderPath + FileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                   break;
               case ".xlsx": //Excel 07
                   conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FolderPath + FileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
                   break;
           }
           try
           {
               using (var context = new LQTransAgentSeaFreightRateDataContext())
               {
                   string sql = string.Format("Select * FROM [{0}]" ,  ddlSheets.SelectedValue);
                   using (var myConnection = new OleDbConnection(conStr))
                   using (var myCommand = new OleDbCommand(sql, myConnection))
                   {
                       myConnection.Open();
                       var myReader = myCommand.ExecuteReader();
                       while (myReader.Read())
                       {
                           context.TB_TransAgentSeaFreightRates.InsertOnSubmit(new TB_TransAgentSeaFreightRate()
                           {
                               tASF_VCPOD = myReader.GetString(0),
                               tASF_VCPOL = myReader.GetString(1),
                               tASF_VCForwarder = myReader.GetString(2),
                               tASF_VCForwarderReference = myReader.GetString(3),
                               tASF_VCShippingLine = myReader.GetString(4),
                               tASF_VCContainerType = myReader.GetString(5),
                               tASF_VCContainerSize = myReader.GetString(6),
                               tASF_DTEValidFrom = Convert.ToDateTime(myReader.GetString(7)),
                               tASF_DTEValidTo = Convert.ToDateTime(myReader.GetString(8)),
                               tASF_NUBasicRate = mobjGenlib.ConvertLong(myReader.GetString(9)),
                               tASF_NUPAF = mobjGenlib.ConvertLong(myReader.GetString(10)),
                               tASF_NUCAF = mobjGenlib.ConvertLong(myReader.GetString(11)),
                               tASF_NUPSS = mobjGenlib.ConvertLong(myReader.GetString(12)),
                               tASF_NUTotalAmount = mobjGenlib.ConvertLong(myReader.GetString(13)),
                               tASF_NUFreeDays = mobjGenlib.ConvertLong(myReader.GetString(14)),
                               tASF_VCCreditDays = myReader.GetString(15),
                               tASF_VCNITDeposit = myReader.GetString(16),
                               tASF_NUIsActive = 1,
                               tASF_mCMP_NUUniqueId = mobjGenlib.ConvertLong(TXTCompanyID.Text)
                           });
                       }
                   }

                   context.SubmitChanges();
               }
           }
           catch (Exception ex)
           {
               lblMessage.ForeColor = System.Drawing.Color.White;
               lblMessage.Text = ex.Message;
           }
       }

I don't have an idea to how to make it to work fine...if any one suggest me it would be very helpful. 我不知道如何使它正常工作...如果有人建议我会很有帮助。

Thanks in adavance. 非常感谢。

The error was caused by improper unboxing. 该错误是由于装箱不当引起的。 (Ref: http://msdn.microsoft.com/en-us/library/b95fkada(v=vs.80).aspx ) (参考: http//msdn.microsoft.com/en-us/library/b95fkada (v = vs.80.aspx

Response Limitations: I don't know the property types for TB_TransAgentSeaFreightRate. 响应限制:我不知道TB_TransAgentSeaFreightRate的属性类型。

Assumption: The types of the data being assigned to the properties matches the types of 假设:分配给属性的数据类型与
the properties. 属性。

There are 2 ways, that I see, of handling this from a debugging standpoint 从调试的角度来看,我有两种处理方式

  1. Given the way your are doing this, you won't be able to see the exact failure. 考虑到您执行此操作的方式,您将无法看到确切的故障。 You could temporarily move the initialization of the members out of the new statement. 您可以暂时将成员的初始化移出新语句。

  2. Alternatively, you could debug through the code repetitively each time commenting out 1 line from the assignments until you have found the offending line. 另外,您可以每次从赋值中注释掉1行,直到发现有问题的行,然后重复调试代码。

I am discounting Convert.ToDateTime as the source of the problem. 我不赞成将Convert.ToDateTime作为问题的根源。 It throws a FormatException for and invalid DateTime. 它将引发FormatException和无效的DateTime。 Ref. 参考 http://msdn.microsoft.com/en-us/library/xhz1w05e(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/xhz1w05e(v=vs.110).aspx

My guess is that it is occurring on one of the mobjGenlib.ConvertLong. 我的猜测是它发生在mobjGenlib.ConvertLong之一上。 You're going to have to debug through the code to find the line in question. 您将不得不调试代码以找到有问题的行。 Also keep in mind that this error is happening because of the data. 还请记住,由于数据而导致发生此错误。 You will need to account for this type of error, and deal with it in the code. 您将需要解决这种类型的错误,并在代码中进行处理。

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

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