请不要将此标记为重复问题。 我一直在尝试按照我找到的所有答案进行操作,但我仍然遇到此错误。谁能让我知道这个错误来自哪里?
这是错误
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 :(得分:4)
您的requestID是long
,您要将其转换为Int32
。将其更改为Int64
。
RequestID = reader.GetInt64(0)
,PartID = reader.GetInt64(15)