获得强制转换例外

时间:2015-08-17 19:31:17

标签: c# asp.net linq

我正在使用Linq 2 SQL,并且在触发按钮单击事件“指定的强制转换无效”时收到此错误消息,堆栈跟踪如下所示。我是新手使用linq到sql并尝试远离ADO.NET,顺便说一句,使用ADO.NET,调用存储过程。

  

[InvalidCastException:指定的强制转换无效。]      System.Data.SqlClient.SqlBuffer.get_Decimal()+ 5299638      System.Data.SqlClient.SqlDataReader.GetDecimal(Int32 i)+68      Read_GetBreakDownsResult(ObjectMaterializer 1 ) +3730 System.Data.Linq.SqlClient.ObjectReader 2.MoveNext()+ 32      System.Collections.Generic.List 1..ctor(IEnumerable 1个集合)+381      System.Linq.Enumerable.ToList(IEnumerable`1 source)+58      GuhringWebsite2.ReportingPortal.Default.LoadBreakDowns(Object sender,EventArgs e)位于c:\ WillieDevonSmith \ GuhringWebsite2 \ Reporting \ Reports \ Default.aspx.cs:51      System.Web.UI.WebControls.Button.OnClick(EventArgs e)+9628722      System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+103      System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+10      System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13      System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+35      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1724

这是使用linq 2 sql调用存储过程的代码,我将其传递给静态值进行测试。

    ReportsDataContext reportsdbContext = new ReportsDataContext();

    var query = reportsdbContext.GetBreakDowns("0070917819");
    GridView1.DataSource = query;
    GridView1.DataBind();

在designer.cs类中,它在这里得到了解决

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetBreakDowns")]
    public ISingleResult<GetBreakDownsResult> GetBreakDowns([global::System.Data.Linq.Mapping.ParameterAttribute(Name="BillToCustomerNo", DbType="NVarChar(20)")] string billToCustomerNo)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), billToCustomerNo);
        return ((ISingleResult<GetBreakDownsResult>)(result.ReturnValue));
    }

我的存储过程看起来像这样

CREATE PROCEDURE [dbo].[GetBreakDowns] 
-- Add the parameters for the stored procedure here
@BillToCustomerNo nvarchar(20)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT DivName,SUM(CASE WHEN BillingDateYY = '2010' THEN totalvalue ELSE 0 END) 
AS '2010',SUM(CASE WHEN BillingDateYY = '2011' THEN totalvalue ELSE 0 END) 
    AS '2011',SUM(CASE WHEN BillingDateYY = '2012' THEN totalvalue ELSE 0 END)
        AS '2012',SUM(CASE WHEN BillingDateYY = '2013' THEN totalvalue ELSE 0 END)
            AS '2013',SUM(CASE WHEN BillingDateYY = '2014' THEN totalvalue ELSE 0 END)   
                AS '2014', sum(totalvalue) FROM  [ReportData].[dbo].[Sales] WHERE BillToCustomerNo in (@BillToCustomerNo) 
                    GROUP BY DivName
UNION 
SELECT 'Total'As 'DivName',SUM(CASE WHEN BillingDateYY = '2010' THEN totalvalue ELSE 0 END) 
    AS '2010',SUM(CASE WHEN BillingDateYY = '2011' THEN totalvalue ELSE 0 END) 
        AS '2011',SUM(CASE WHEN BillingDateYY = '2012' THEN totalvalue ELSE 0 END) 
            AS '2012',SUM(CASE WHEN BillingDateYY = '2013' THEN totalvalue ELSE 0 END)
                AS '2013',SUM(CASE WHEN BillingDateYY = '2014' THEN totalvalue ELSE 0 END)
                    AS '2014',sum(totalvalue) FROM  [ReportData].[dbo].[Sales] WHERE BillToCustomerNo in (@BillToCustomerNo)
END

0 个答案:

没有答案