输入字符串的格式不正确 [英] Input string was not in correct format

查看:98
本文介绍了输入字符串的格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gridview中动态绑定id。



代码如下



< asp:GridView ID =GridView1runat =serverAllowPaging =TrueAutoGenerateColumns =FalseCellPadding =10

CellSpacing =0OnRowDataBound =GridView1_RowDataBound

/>

< columns>

< asp:BoundField DataField =DateHeaderText =Date/>

< asp:BoundField DataField =LocationHeaderText =Location/>

< asp:BoundField DataField =TimeInHeaderText =TimeIn/>

< asp:BoundField DataField =TimeOutHeaderText =TimeOut/>

< asp:BoundField DataField =WorkingHoursHeaderText =WorkingHours/ >

< asp:TemplateField HeaderText =ViewAttendance>

< itemtemplate>



< asp:LinkBut​​ton ID =lnkdeletehref =#myModaldata-toggle =modalrunat =server>点击这里







< asp:TemplateField HeaderText =Id>

< itemtemplate>

<%# Container.DataItemIndex + 1%>











gridview行数据绑定代码如下



protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

{

if(e.Row.RowType == DataControlRowType.DataRow)

{

string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem,Date) ));

LinkBut​​ton lnkbtnresult =(LinkBut​​ton)e.Row.FindControl(lnkdelete);

BindGrid2();

}



if(e.Row.RowType == DataControlRowType.DataRow)

{

int id =转换。 ToInt32(e.Row.Cells [6] .Text);

if(id == 2)

{

BindGrid3();

gvview2.Visible = true;

}

}



日期位置Timein TimeOut Workinghours ViewAttendance Id



1/1/2018 Chennai 9.30 6.30 9 Clickhere 1

2 // 12018 Chennai 9.30 7.30 10 Clickh ere 2

3/1/2018 Chennai 9.30 5.30 8 Clickhere 3

4/1/2018 Chennai 9.30 8.00 10.30 Clickhere 4

5 / 1/2018 Chennai 9.30 7.00 9.30 Clickhere 5



当我运行上面的代码时显示错误如下



输入字符串格式不正确



以上错误如下所示:



int id = Convert.ToInt32(e.Row.Cells [6] .Text);



上面代码中的错误是什么。



我尝试过:



dynamically binding id in gridview.

code as follows

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="10"
CellSpacing="0" OnRowDataBound="GridView1_RowDataBound"
/>
<columns>
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="TimeIn" HeaderText="TimeIn" />
<asp:BoundField DataField="TimeOut" HeaderText="TimeOut" />
<asp:BoundField DataField="WorkingHours" HeaderText="WorkingHours" />
<asp:TemplateField HeaderText="ViewAttendance">
<itemtemplate>

<asp:LinkButton ID="lnkdelete" href="#myModal" data-toggle="modal" runat="server">Click Here



<asp:TemplateField HeaderText="Id">
<itemtemplate>
<%# Container.DataItemIndex + 1 %>





gridview row data bound code as follows

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Date"));
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
BindGrid2();
}

if (e.Row.RowType == DataControlRowType.DataRow)
{
int id = Convert.ToInt32(e.Row.Cells[6].Text);
if (id == 2)
{
BindGrid3();
gvview2.Visible = true;
}
}

Date Location Timein TimeOut Workinghours ViewAttendance Id

1/1/2018 Chennai 9.30 6.30 9 Clickhere 1
2//12018 Chennai 9.30 7.30 10 Clickhere 2
3/1/2018 Chennai 9.30 5.30 8 Clickhere 3
4/1/2018 Chennai 9.30 8.00 10.30 Clickhere 4
5/1/2018 Chennai 9.30 7.00 9.30 Clickhere 5

When i run the above code shows error as follows

The input string was not in correct format

The above error shows in below line as follows

int id = Convert.ToInt32(e.Row.Cells[6].Text);

what is the mistake in my above code.

What I have tried:

 dynamically binding id in gridview.

 code as follows

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="10"
CellSpacing="0" OnRowDataBound="GridView1_RowDataBound" 
       />
         <Columns>
                 <asp:BoundField DataField="Date" HeaderText="Date" />  
                 <asp:BoundField DataField="Location" HeaderText="Location" />  
                 <asp:BoundField DataField="TimeIn" HeaderText="TimeIn" />  
                 <asp:BoundField DataField="TimeOut" HeaderText="TimeOut" />
                 <asp:BoundField DataField="WorkingHours" HeaderText="WorkingHours" />
            <asp:TemplateField HeaderText="ViewAttendance">
                <ItemTemplate>

                  <asp:LinkButton ID="lnkdelete" href="#myModal" data-toggle="modal" runat="server">Click Here</asp:LinkButton>
               </ItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField HeaderText="Id">
                 <ItemTemplate>
                     <%# Container.DataItemIndex + 1 %>
                 </ItemTemplate>
             </asp:TemplateField>
        </Columns>
    </asp:GridView>

   gridview row data bound code as follows

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
       if (e.Row.RowType == DataControlRowType.DataRow)
        {
         string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Date"));
         LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
           BindGrid2();
       }

         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             int id = Convert.ToInt32(e.Row.Cells[6].Text);
             if (id == 2)
             {
                 BindGrid3();
                 gvview2.Visible = true;
             }
         }

         Date      Location   Timein   TimeOut   Workinghours   ViewAttendance     Id
 
         1/1/2018   Chennai    9.30      6.30          9          Clickhere         1
         2//12018   Chennai    9.30      7.30          10         Clickhere         2
         3/1/2018   Chennai    9.30      5.30          8          Clickhere         3
         4/1/2018   Chennai    9.30      8.00          10.30      Clickhere         4
         5/1/2018   Chennai    9.30      7.00          9.30       Clickhere         5

 When i run the above code shows error as follows

 The input string was not in correct format

 The above error shows in below line as follows
 
 int id = Convert.ToInt32(e.Row.Cells[6].Text);

 what is the mistake in my above code.

推荐答案

正如其他人已经建议的那样确实使用有利可图 调试器。这意味着你必须在运行时检查什么是有问题的 e.Row.Cells [6] .Text 值。

另一种方法是处理报告有用信息的异常(同样是不可转换的值)。
As others already suggested do use profitably the debugger. That means you have to check at runtime what is the offending e.Row.Cells[6].Text value.
An alternative would be handling the exception in order to report useful information (again the non-convertible value).


如果使用调试器,你会看到e.Row.Cells [6] .Text是一个空字符串因此无法转换为int。学习使用调试器是一个必不可少的工具,你不能继续倾倒大量代码,总是认为别人可以告诉你问题是什么。



短回答你的问题就是用这个来代替



If you use the debugger you'll see that e.Row.Cells[6].Text is an empty string so can't be converted to an int. Learning to use the debugger is an essential tool, you can't keep dumping reams of code and always think someone else can tell you what the problem is.

The short answer to your question is to use this instead

int id = e.Row.RowIndex;





长答案是具有ID的单元格是模板字段,模板字段包含控件,而不是纯文本。如果你看一下定义;





The long answer is that the cell with the ID is a template field and template fields contain controls, not plain text. If you look at the definition;

<asp:TemplateField HeaderText="Id">
    <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
    </ItemTemplate>
</asp:TemplateField>





你可以看到你实际上并没有添加任何控件,无论是asp。 net正在做一些工作并为你添加控件,它将你的ItemTemplate中的所有内容转换为asp:DataBoundLiteralControl。所以如果你使用调试器并查看



e.Row.Cells [6] .Controls



你会看到那里有一个控件,一个数据绑定文字,你可以用它来获得这个





You can see that you're not actually adding any controls, however asp.net is doing some work and adding the control for you, it turns everything in your ItemTemplate into an asp:DataBoundLiteralControl. So if you use the debugger and look at

e.Row.Cells[6].Controls

you'll see there is one control in there, a databound literal, which you can get using this

DataBoundLiteralControl l = (DataBoundLiteralControl) e.Row.Cells[6].Controls[0];





如果你看看l.Text,你现在可以看到单元格中显示的文本。但是你仍然无法将其转换为int,因为它在模板中包含* everything *,包括空格,换行符等。由于html忽略空格和行返回,因此在页面上看不到这些*,但Convert.ToInt32不会t。



你必须记住gridview是一种显示机制,你永远不应该读取单元格的内容来进行处理,因为数据总是以另一种方式可用,在您的特定情况下,数据在e.Row.RowIndex中已经作为int提供,因此您应该使用它而不是解析单元格文本。



if you look at "l.Text" you can now see the text that appears in the cell. However you still can't convert that to int because it contains *everything* in your template including white space, line returns etc. Those aren't seen on the page as html ignores white space and line returns, but Convert.ToInt32 doesn't.

You have to remember that the gridview is a display mechanism, you should never read the contents of the cells to do processing as the data is always available another way, in your specific case the data is available as an int already in e.Row.RowIndex so that is what you should use rather than parsing the cell text.


这篇关于输入字符串的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆