简体   繁体   English

“输入字符串的格式不正确。”

[英]“Input string was not in a correct format.”

I want to calculate the running total of gridview data I have the following code: 我要计算gridview数据的运行总数,我有以下代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRowView dtview = e.Row.DataItem as DataRowView;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblaprl = (Label)e.Row.FindControl("lblaprl");
        Totalapr = Totalapr + Convert.ToDecimal(lblaprl.Text);
        Label lblmay = (Label)e.Row.FindControl("lblmay");
        Totalmay = Totalmay + Convert.ToDecimal(lblmay.Text);
    }

    if (e.Row.RowType == DataControlRowType.Footer)
    {
            Label lbl_aprTotalamount = (Label)e.Row.FindControl("lbl_aprTotalamount");
            lbl_aprTotalamount.Text = Totalapr.ToString();
            Label lbl_mayTotalamount = (Label)e.Row.FindControl("lbl_mayTotalamount");
            lbl_mayTotalamount.Text = Totalmay.ToString();
    }
}

When we run this code for one column for apr month it works fine but when we want to calculate running total of both column separately it's causing this error: 当我们在四月的一列中运行此代码时,它可以正常工作,但是当我们要分别计算两列的运行总计时,则会导致此错误:

Input string was not in a correct format. 输入的字符串格式不正确。

on the following line in my code 在我的代码的以下行

Label lblmay = (Label)e.Row.FindControl("lblmay");
***Totalmay = Totalmay + Convert.ToDecimal(lblmay.Text);***

My c# code for bind gridview 我的C#代码绑定GridView

public void bindgvduedate()
{
    con = new SqlConnection();
    con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
    con.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "readfeedetails";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = con;
    ////cmd.Parameters.AddWithValue("@classname",Convert.ToString(ddlclassname.SelectedItem));
    ////cmd.Parameters.AddWithValue("@feecatename", Convert.ToString(ddlstcategory.SelectedItem));

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable table = new DataTable();
    da.Fill(table );
    GridView1.DataSource= table;
    GridView1.DataBind();
    con.Close();
}

This function I call on page load event. 我在页面加载事件上调用此函数。

Gridview aspx markup: GridView Aspx标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     BackColor="White" BorderColor="#336666" BorderStyle="Double" 
     Font-Size="11px" BorderWidth="3px" 
     CellPadding="4" GridLines="both" 
     ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
     <RowStyle ForeColor="#333333" BackColor="White" Height="21px" />
     <Columns>
         <asp:TemplateField>
             <HeaderTemplate>Apr-15 </HeaderTemplate>
             <ItemTemplate><asp:Label ID="lblaprl" runat="server" Text='<%#Eval("Apr-15") %>'></asp:Label></ItemTemplate>
             <FooterTemplate> <asp:Label ID="lbl_aprTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>May-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lblmay" runat="server" Text='<%#Eval("May-15")%>'></asp:Label></ItemTemplate>
            <FooterTemplate><asp:Label ID="lbl_mayTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label></FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Jun-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lbljun" runat="server" Text='<%#Eval("Jun-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_junTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Jul-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lbljul" runat="server" Text='<%#Eval("Jul-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_julTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Aug-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lblaug" runat="server" Text='<%#Eval("Aug-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_augTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Sep-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lblsep" runat="server" Text='<%#Eval("Sep-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_sepTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Oct-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lbloct" runat="server" Text='<%#Eval("Oct-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_octTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Nov-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lblnov" runat="server" Text='<%#Eval("Nov-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_novTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Dec-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lbldec" runat="server" Text='<%#Eval("Dec-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_decTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Jan-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lbljan" runat="server" Text='<%#Eval("Jan-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_janTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Feb-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lblfeb" runat="server" Text='<%#Eval("Feb-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_febTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Mar-15 </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lblmar" runat="server" Text='<%#Eval("Mar-15") %>'></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_marTotalamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
            <HeaderTemplate>Total </HeaderTemplate>
            <ItemTemplate><asp:Label ID="lbltotal" runat="server" Text="0"></asp:Label></ItemTemplate>
            <FooterTemplate> <asp:Label ID="lbl_Totalsamount" CssClass="footer_text" runat="server" Text="1"></asp:Label> </FooterTemplate>
         </asp:TemplateField>
     </Columns>
     <FooterStyle BackColor="White" ForeColor="#333333" />
     <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
     <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>

My database table 我的数据库表

CREATE TABLE [dbo].[feemaster]
(
   [feeId] [int] IDENTITY(1,1) NOT NULL,
   [feetype] [nvarchar](50) NULL,
   [feename] [nvarchar](50) NULL,
   [classname] [nvarchar](50) NULL,
   [duemonth] [nvarchar](50) NULL,
   [feeamount] [decimal](15, 2) NULL,
   [feeactive] [bit] NULL,
   [feecatename] [nvarchar](50) NULL
) ON [PRIMARY]
GO

SET IDENTITY_INSERT [dbo].[feemaster] ON

INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) 
VALUES (1, N'One Time', N'Prospectus & Registration Fee', N'PRE NURSURY', N'Apr-15', CAST(300.00 AS Decimal(15, 2)), 1, N'NEW')

INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) 
VALUES (13, N'One Time', N'Prospectus & Registration Fee', N'NURSURY', N'Apr-15', CAST(300.00 AS Decimal(15, 2)), 1, N'NEW')

INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) 
VALUES (2, N'One Time', N'Prospectus & Registration Fee', N'KG', N'Apr-15', CAST(300.00 AS Decimal(15, 2)), 1, N'NEW')

INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) 
VALUES (16, N'Annual', N'Maintenance Fee', N'NURSURY', N'Apr-15', CAST(500.00 AS Decimal(15, 2)), 1, N'NEW')

INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) 
VALUES (17, N'Annual', N'Maintenance Fee', N'NURSURY', N'Apr-15', CAST(500.00 AS Decimal(15, 2)), 1, N'OLD')

INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) 
VALUES (18, N'Annual', N'Maintenance Fee', N'KG', N'Apr-15', CAST(500.00 AS Decimal(15, 2)), 1, N'NEW')

My stored procedure 我的存储过程

create proc [dbo].[readfeedetails]
AS
BEGIN
    select
        *
    from
        (select 
             f.feename, f.feeamount, dd.duemonth 
         from 
             feemaster as f
         left outer join 
             duedate as dd on dd.duemonth = f.duemonth  
         where   
             classname = 'KG' and feeactive = 1 
             and feecatename = 'OLD') as f
pivot
(
  min(f.feeamount)
  for f.duemonth in ([Apr-15],[May-15],[Jun-15],[Jul-15],[Aug-15],[Sep-  15],[Oct-15],[Nov-15],[Dec-15],[Jan-15],[Feb-15],[Mar-15])
) as PIV
END
GO

Please can anyone suggest how we can remove this error? 请任何人建议我们如何删除此错误?

I guess in some rows,lblmay is empty. 我猜在某些行中,lblmay是空的。 Try to check this condition,something like this : 尝试检查这种情况,如下所示:

    Label lblmay = (Label)e.Row.FindControl("lblmay");
    if (lblmay.Text.Trim()!="")
    {
       Totalmay = Totalmay + Convert.ToDecimal(lblmay.Text);
    }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM