【问题标题】:Error in SELECT command after changing date更改日期后 SELECT 命令出错
【发布时间】:2013-02-13 07:26:01
【问题描述】:

先生,我正在使用以下脚本(完整的 aspx 页面)从 2012 年 12 月的 SQL 服务器 2008 数据库中删除 3 个数据透视表

 <%@ Page Language="C#" %>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <script runat="server">

 </script>

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
    .style1
    {
        color: #800000;
        font-size: medium;
        font-family: "Comic Sans MS";
    }
    .style2
    {
        text-align: center;
        color: #0000CC;
        font-family: "Comic Sans MS";
        font-size: small;
    }
    .style3
    {
        font-size: large;
    }
    .style4
    {
        text-align: center;
        color: #800000;
        font-family: "Comic Sans MS";
        font-size: small;
    }
    .style5
    {
        font-size: small;
    }
</style>

<script runat="server" visible="True">
int TotalTTL1;
int GetTTL1(int Train)
{
    TotalTTL1 += Train;
    return Train;
}
int GetTotalTTL1()
{
    return TotalTTL1;
}

</script>

</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">



<p class="style4">
    <strong>Three Phase Locomotive</strong></p>
<asp:GridView ID="GridView2" runat="server" AllowSorting="True" 
    AutoGenerateColumns="False" BorderColor="#CC0000" BorderStyle="Solid" 
    BorderWidth="2px" DataSourceID="SqlDataSource2" HorizontalAlign="Center" 
    ShowFooter="True" style="margin-top: 0px">
    <Columns>
        <asp:TemplateField HeaderText="Equipment" SortExpression="Equipt" 
            FooterText="Grand Total">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Equipt") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Equipt") %>'></asp:Label>
            </ItemTemplate> 
            <FooterStyle Font-Bold="True" />
           <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:TemplateField>
        <asp:BoundField DataField="BSL" HeaderText="BSL" ReadOnly="True" 
            SortExpression="BSL" />
        <asp:BoundField DataField="AQ" HeaderText="AQ" ReadOnly="True" 
            SortExpression="AQ" />
        <asp:BoundField DataField="KYN" HeaderText="KYN" ReadOnly="True" 
            SortExpression="KYN" />
        <asp:BoundField DataField="ASN" HeaderText="ASN" ReadOnly="True" 
            SortExpression="ASN" />
        <asp:BoundField DataField="HWH" HeaderText="HWH" ReadOnly="True" 
            SortExpression="HWH" />
        <asp:BoundField DataField="MGS" HeaderText="MGS" ReadOnly="True" 
            SortExpression="MGS" />
        <asp:BoundField DataField="GMO" HeaderText="GMO" ReadOnly="True" 
            SortExpression="GMO" />
        <asp:BoundField DataField="WAT" HeaderText="WAT" ReadOnly="True" 
            SortExpression="WAT" />
        <asp:BoundField DataField="ANGL" HeaderText="ANGL" ReadOnly="True" 
            SortExpression="ANGL" />
        <asp:BoundField DataField="GZB" HeaderText="GZB" ReadOnly="True" 
            SortExpression="GZB" />
        <asp:BoundField DataField="LDH" HeaderText="LDH" ReadOnly="True" 
            SortExpression="LDH" />
        <asp:BoundField DataField="JHS" HeaderText="JHS" ReadOnly="True" 
            SortExpression="JHS" />
        <asp:BoundField DataField="CNB" HeaderText="CNB" ReadOnly="True" 
            SortExpression="CNB" />
        <asp:BoundField DataField="AJJ" HeaderText="AJJ" ReadOnly="True" 
            SortExpression="AJJ" />
        <asp:BoundField DataField="ED" HeaderText="ED" ReadOnly="True" 
            SortExpression="ED" />
        <asp:BoundField DataField="RPM" HeaderText="RPM" ReadOnly="True" 
            SortExpression="RPM" />
        <asp:BoundField DataField="BZA" HeaderText="BZA" ReadOnly="True" 
            SortExpression="BZA" />
        <asp:BoundField DataField="LGD" HeaderText="LGD" ReadOnly="True" 
            SortExpression="LGD" />
        <asp:BoundField DataField="KZJ" HeaderText="KZJ" ReadOnly="True" 
            SortExpression="KZJ" />
        <asp:BoundField DataField="TATA" HeaderText="TATA" ReadOnly="True" 
            SortExpression="TATA" />
        <asp:BoundField DataField="BNDM" HeaderText="BNDM" ReadOnly="True" 
            SortExpression="BNDM" />
        <asp:BoundField DataField="BKSC" HeaderText="BKSC" ReadOnly="True" 
            SortExpression="BKSC" />
        <asp:BoundField DataField="SRC" HeaderText="SRC" ReadOnly="True" 
            SortExpression="SRC" />
        <asp:BoundField DataField="BIA" HeaderText="BIA" ReadOnly="True" 
            SortExpression="BIA" />
        <asp:BoundField DataField="BRC" HeaderText="BRC" ReadOnly="True" 
            SortExpression="BRC" />
        <asp:BoundField DataField="BL" HeaderText="BL" ReadOnly="True" 
            SortExpression="BL" />
        <asp:BoundField DataField="TKD" HeaderText="TKD" ReadOnly="True" 
            SortExpression="TKD" />
        <asp:BoundField DataField="ET" HeaderText="ET" ReadOnly="True" 
            SortExpression="ET" />
        <asp:BoundField DataField="NKJ" HeaderText="NKJ" ReadOnly="True" 
            SortExpression="NKJ" />
        <asp:TemplateField HeaderText="Total" SortExpression="TTL">
             <ItemTemplate>
                    <%# GetTTL1(int.Parse(Eval("TTL").ToString())).ToString()%>
                </ItemTemplate>
                <FooterTemplate>
                    <%# GetTotalTTL1().ToString()%>
                </FooterTemplate>
             <FooterStyle Font-Bold="True" />
        </asp:TemplateField>
    </Columns>
    <FooterStyle Font-Names="Tahoma" Font-Size="Small" ForeColor="Black" 
        HorizontalAlign="Center" VerticalAlign="Middle" />
    <HeaderStyle Font-Names="Tahoma" Font-Size="Small" ForeColor="Black" 
        HorizontalAlign="Center" VerticalAlign="Middle" />
    <RowStyle Font-Names="Tahoma" Font-Size="Small" HorizontalAlign="Center" 
        VerticalAlign="Middle" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>" 
    SelectCommand="SELECT  Equipt, NULLIF([BSL],0) AS BSL, NULLIF([AQ],0) AS AQ, NULLIF([KYN],0) AS KYN,  NULLIF([ASN],0) AS ASN, NULLIF([HWH],0) AS HWH, NULLIF([MGS],0) AS MGS, NULLIF([GMO],0) AS GMO, NULLIF([WAT],0) AS WAT, NULLIF([ANGL],0) AS ANGL, NULLIF([GZB],0) AS GZB, NULLIF([LDH],0) AS LDH, NULLIF([JHS],0) AS JHS, NULLIF([CNB],0) AS CNB, NULLIF([AJJ],0) AS AJJ, NULLIF([ED],0) AS ED, NULLIF([RPM],0) AS RPM, NULLIF([BZA],0) AS BZA, NULLIF([LGD],0) AS LGD, NULLIF([KZJ],0) AS KZJ, NULLIF([TATA],0) AS TATA, NULLIF([BNDM],0) AS BNDM, NULLIF([BKSC],0) AS BKSC, NULLIF([SRC],0) AS SRC, NULLIF([BIA],0) AS BIA, NULLIF([BRC],0) AS BRC, NULLIF([BL],0) AS BL, NULLIF([TKD],0) AS TKD, NULLIF([ET],0) AS ET, NULLIF([NKJ],0) AS NKJ, NULLIF(([BSL] + [AQ] + [KYN] + [ASN] + [HWH] + [MGS] + [GMO] + [WAT] + [ANGL] + [GZB] + [LDH] + [JHS] + [CNB] + [AJJ] + [ED] + [RPM] + [BZA] + [LGD] + [KZJ] + [TATA] + [BNDM] + [BKSC] + [SRC] + [BIA] + [BRC] + [BL] + [TKD] + [ET] + [NKJ] ),0) AS TTL 
 FROM 
( 
SELECT Equipt, Shed 
FROM ERfailures WHERE Date &gt;= '12/01/2012' AND Date &lt;= '12/31/2012' AND loco_type = '3-Phase' AND classification = 'Loco'  
) x
PIVOT 
( 
COUNT(Shed) 
FOR Shed IN ([BSL],  [AQ],  [KYN],  [ASN],  [HWH],  [MGS],  [GMO],  [WAT],  [ANGL],  [GZB],  [LDH],  [JHS],  [CNB],  [AJJ],  [ED],  [RPM],  [BZA],  [LGD],  [KZJ],  [TATA],  [BNDM],  [BKSC],  [SRC],  [BIA],  [BRC],  [BL],  [TKD],  [ET],  [NKJ] ))  p">
</asp:SqlDataSource>




</form>
<p class="style5" style="text-align: center">
    <a href="mainpage.aspx">Back</a></p>
 </body>
 </html>

现在我想将 2013 年 1 月的行更改为

   FROM ERfailures WHERE Date &gt;= '01/01/2013' AND Date &lt;= '01/31/2013' AND loco_type = '3-Phase' AND classification = 'Loco'

它给出一个错误

  Input string was not in a correct format."
  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.FormatException: Input string was not in a correct format.
 Stack Trace: 


[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +9591147
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
ASP.erequipfailurejanuary_aspx.__DataBind__control43(Object sender, EventArgs e) +106
System.Web.UI.Control.OnDataBinding(EventArgs e) +91
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +92
System.Web.UI.Control.DataBind() +15
System.Web.UI.Control.DataBindChildren() +201
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +101
System.Web.UI.Control.DataBind() +15
System.Web.UI.Control.DataBindChildren() +201
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +101
System.Web.UI.Control.DataBind() +15
System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +166
System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3896
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +66
System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75
System.Web.UI.Control.EnsureChildControls() +102
System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496"

如何解决这个问题?请帮忙。

【问题讨论】:

  • 给出错误的不是 SQL 命令本身 - 绑定时 GridView 失败 - 其中一个值的格式不正确(对于 Int32,它会出现)。

标签: string sql-server-2008 selectcommand


【解决方案1】:

问题不在于您的查询。这是您要返回的值之一。看起来您的绑定目标中有一个列需要一个数字,但得到一个非数字值。

基于:

[FormatException: 输入字符串的格式不正确。] System.Number.StringToNumber(String str, NumberStyles 选项, NumberBuffer& 数字, NumberFormatInfo 信息, Boolean parseDecimal) +9591147 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119 ASP.erequipfailurejanuary_aspx._DataBind_control43(Object sender, 事件参数 e)

【讨论】:

  • 感谢您的回复。上面的代码正在使用日期,例如“FROM ERfailures WHERE Date >= '12/01/2012' AND Date
  • 如果从 Gridview 中删除页脚总和脚本(Total 或 TTL 字段),新日期(2013 年 1 月)将起作用。
  • 因为有效的日期不包括结果集中的错误行。我建议将绑定目标中的列类型与您正在查询的结果集中的数据类型相匹配。如果该列在数据库中不是 int,那么您无法确定任何行的值都是 int。
  • 你是对的。错误是由于从 excel 工作表导入的 sql 表数据中存在一些奇怪的字符。
猜你喜欢
  • 2016-01-26
  • 1970-01-01
  • 2010-09-21
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
相关资源
最近更新 更多