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);
}
我希望有人能帮助我解决这个错误!
它看起来像调用reader.GetInt32(0)时发生错误。 这只是一个调试问题:SqlDataReader希望在您的数据库中找到一个int,但该位置上的某些内容不能转换为整数。 尝试设置断点,以便您可以调查这是否发生在每条记录或只是(a)某些记录。某处可能有某些错误数据。 –
您的requestid很长,您正在为其分配一个Int32值。 – Mairaj
什么将分配? @MairajAhmad .. – Anaiah