从下拉列表中选择值时,输入字符串格式不正确

时间:2014-07-16 07:43:27

标签: asp.net c#-4.0

我的代码抛出错误:

输入字符串格式不正确

代码:

 protected void btnGenerateReport_Click(object sender, EventArgs e)
    {
        dtRoom.Columns.Add(new DataColumn("ID", typeof(string)));
        dtRoom.Columns.Add(new DataColumn("RecievingDate", typeof(string)));
        dtRoom.Columns.Add(new DataColumn("FromMobileNo", typeof(string)));
        dtRoom.Columns.Add(new DataColumn("Message", typeof(string)));
        dtRoom.Columns.Add(new DataColumn("IsComplaint", typeof(short)));
       // bool var = false;
        for (int i = 0; i <= 5; i++)
        {
            //drw = dtRoom.NewRow();
            DropDownList IsValid = (DropDownList) GridViewSmsComplaints.Rows[i].FindControl("ddlValidity");
            if (IsValid.SelectedValue == "1")
            {
                int ID = Convert.ToInt32(GridViewSmsComplaints.Rows[i].Cells[0].Text);
                ManageRecievedMessage mngRecMsg = new ManageRecievedMessage();
                mngRecMsg.UpdateSmsComplaintValidity(ID, 1);

                //var = true;
                DataRow datarw = null;
                //dtRoom = new DataTable();
                datarw = dtRoom.NewRow();

                    datarw[0] = GridViewSmsComplaints.Rows[i].Cells[0].Text;
                    datarw[1] = GridViewSmsComplaints.Rows[i].Cells[1].Text;
                    datarw[2] = GridViewSmsComplaints.Rows[i].Cells[2].Text;
                    datarw[3] = GridViewSmsComplaints.Rows[i].Cells[3].Text;
                    datarw[4] =  Convert.ToInt16(GridViewSmsComplaints.Rows[i].Cells[4].Text);

                dtRoom.Rows.Add(datarw);

它在这一行引发错误:

 datarw[4] =  Convert.ToInt16(GridViewSmsComplaints.Rows[i].Cells[4].Text);

它应该将1传递给datarw 4,因为我从下拉列表中选择它。

<asp:DropDownList ID="ddlValidity" runat="server">
                           <asp:ListItem Value="-1" Text="-Select-"></asp:ListItem>
                           <asp:ListItem Value="1" Text="Valid"></asp:ListItem>
                           <asp:ListItem Value="0" Text="Invalid"></asp:ListItem>
                       </asp:DropDownList>

1 个答案:

答案 0 :(得分:1)

您的问题是因为Value为1,但Text对应的是{#1};有效&#34;。您正在传递Text,这不是数字,因此您会收到错误消息。因此,您应该更改代码以使用下拉列表中的Value,如下所示:

datarw[4] =  Convert.ToInt16(IsValid.SelectedValue);