2015-05-04 43 views
0

請不要將此標記爲DUPLICATE問題。 我一直在試圖按照我找到的所有答案,但我仍然得到這個錯誤。任何人都可以告訴我這個錯誤來自哪裏?獲取'指定的轉換無效'。錯誤在MVC SqlDataReader

這裏的錯誤

Server Error in '/NomsPR' Application. 

Specified cast is not valid. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Specified cast is not valid. 

Source Error: 


Line 36:      while (reader.Read()) 
Line 37:      { 
Line 38:        results.Add(new NomsPRItem() 
Line 39:        { 
Line 40:         RequestID = reader.GetInt32(0) 

Source File: c:\SVN\branches\NomsPRMonitoring\NomsPRMonitoring\Models\CheckerConnection.cs Line: 38 

Stack Trace: 


[InvalidCastException: Specified cast is not valid.] 
    System.Data.SqlClient.SqlBuffer.get_Int32() +6639748 
    Lear.NomsPRMonitoring.Models.CheckerConnection.LoadPRItems(DateTime from, DateTime to) in c:\SVN\branches\NomsPRMonitoring\NomsPRMonitoring\Models\CheckerConnection.cs:38 
    Lear.NomsPRMonitoring.Controllers.CheckerController.GetList() in c:\SVN\branches\NomsPRMonitoring\NomsPRMonitoring\Controllers\CheckerController.cs:33 
    lambda_method(Closure , ControllerBase , Object[]) +79 
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +261 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 
    System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +34 
    System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +124 
    System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +838499 
    System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15 
    System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +33 
    System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +839052 
    System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28 
    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15 
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65 
    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15 
    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51 
    System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42 
    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288 

這裏是我的模型:

public class NomsPRItem 
    { 
     public long RequestID { get; set; } 
     public long PartID { get; set; } 
     public string PartNumber { get; set; } 
     public string PartDesc { get; set; } 
     public string UnitName { get; set; } 
     public double PartQuantity { get; set; } 
     public string CurrName { get; set; } 
     public double PiecePrice { get; set; } 
     public DateTime DeliveryDate { get; set; } 
     public string ProposeSuppliers { get; set; } 
     public string Comments { get; set; } 
     public long AccountTypeID { get; set; } 
     public string AccountType { get; set; } 
     public string InboxLearUID { get; set; } 
     public bool ReviewFlag { get; set; } 
     public long SubCatID { get; set; } 
     public string SubCatName { get; set; } 
     public DateTime CreateDate { get; set; } 
     public string CreateBy { get; set; } 
     public DateTime LastDate { get; set; } 
     public string LastBy { get; set; } 
     public string SupplierID { get; set; } 
     public string CostCenter { get; set; } 
     public long SubAccountTypeID { get; set; } 
     public string SubAccountType { get; set; } 

     public double TotalAmount { get; set; } 
     public double Amount { get; set; } 
     public string ItemId { get; set; } 
     public string FullName { get; set; } 
    } 
} 

我的連接

public static List<NomsPRItem> LoadPRItems(DateTime from, DateTime to) 
    { 
     string sSrcipt = m_sReport + "and p.[RequestDate] between '" + from.ToString("yyyy-MM-dd HH:mm:ss") + "' and '" + to.ToString("yyyy-MM-dd HH:mm:ss") + "'"; 
     List<NomsPRItem> results = new List<NomsPRItem>(); 
     using (SqlConnection con = new SqlConnection(m_sConnectionString)) 
     { 
      con.Open(); 
      using (SqlCommand command = new SqlCommand(sSrcipt, con)) 
      { 
       SqlDataReader reader = command.ExecuteReader(); 
       while (reader.Read()) 
       { 
         results.Add(new NomsPRItem() 
         { 
          RequestID = reader.GetInt32(0) 
          ,PartID = reader.GetInt32(15) 
          ,PartDesc = reader.GetString(1) 
          ,PartNumber = reader.GetString(7) 
          ,SupplierID = reader.GetString(16) 
          ,AccountType = reader.GetString(3) 
          ,CurrName = reader.GetString(4) 
          ,PartQuantity = (double)reader.GetDecimal(5) 
          ,PiecePrice = (double)reader.GetDecimal(6) 
          ,Amount = (double)reader.GetDecimal(5) * (double)reader.GetDecimal(6) 
         }); 
       } 
      } 
     } 

     return results; 
    } 
} 

我正在使用本angularjs,所以我這個數據轉換成JSON ..

這裏是我的控制器:

public JsonResult GetList() 
    { 
     DateTime today = DateTime.Now; 
     List<NomsPRItem> model = CheckerConnection.LoadPRItems(new DateTime(today.Year, today.Month, 1, 0, 0, 0), today); 
     return Json(model, JsonRequestBehavior.AllowGet); 
    } 

    public JsonResult GetReportList(string from, string to) 
    { 
     DateTime fromd = DateTime.Now; 
     DateTime tod = DateTime.Now; 
     if (from != "undefined") 
      fromd = Convert.ToDateTime(from); 
     if (to != "undefined") 
      tod = Convert.ToDateTime(to); 
     fromd = new DateTime(fromd.Year, fromd.Month, fromd.Day, 0, 0, 0); 
     tod = new DateTime(tod.Year, tod.Month, tod.Day, 23, 59, 59); 
     return Json(CheckerConnection.LoadPRItems(fromd, tod), JsonRequestBehavior.AllowGet); 
    } 

我希望有人能幫助我解決這個錯誤!

+0

它看起來像調用reader.GetInt32(0)時發生錯誤。 這只是一個調試問題:SqlDataReader希望在您的數據庫中找到一個int,但該位置上的某些內容不能轉換爲整數。 嘗試設置斷點,以便您可以調查這是否發生在每條記錄或只是(a)某些記錄。某處可能有某些錯誤數據。 –

+2

您的requestid很長,您正在爲其分配一個Int32值。 – Mairaj

+0

什麼將分配? @MairajAhmad .. – Anaiah

回答

3

您的請求標識爲long,您將其轉換爲Int32。將其更改爲Int64

RequestID = reader.GetInt64(0) 
,PartID = reader.GetInt64(15) 
+0

很好的回答。 – Mijevoli

相關問題