Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;FormatException未由用户代码“处理”;输入字符串的格式不正确_C#_Asp.net - Fatal编程技术网

C# &引用;FormatException未由用户代码“处理”;输入字符串的格式不正确

C# &引用;FormatException未由用户代码“处理”;输入字符串的格式不正确,c#,asp.net,C#,Asp.net,当我单击“提交”按钮而不在文本框中键入编号时 我收到错误信息 用户代码未处理FormatException,输入字符串不在 格式正确 这是我的密码: protected void btnSubmit_Click(object sender, EventArgs e) { int section = Convert.ToInt32(lstSectionNumber.SelectedValue); double ticketQuantity = Convert.ToInt32(txt

当我单击“提交”按钮而不在文本框中键入编号时

我收到错误信息

用户代码未处理FormatException,输入字符串不在 格式正确

这是我的密码:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int section = Convert.ToInt32(lstSectionNumber.SelectedValue);
    double ticketQuantity = Convert.ToInt32(txtNumberOfTickets.Text);
}
我知道我可以验证文本框,所以我必须输入数字,但有人能告诉我如何正确解决这个问题吗


谢谢,我只使用C#一周。

尝试使用int.TryParse方法

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int section = 0; //or your default value
        double ticketQuantity = 0d; //Or your default value

        int.TryParse(lstSectionNumber.SelectedValue, out section);
        double.TryParse(txtNumberOfTickets.Text, out ticketQuantity);

    }

如果要为此引发异常,请尝试以下操作:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        int section = Convert.ToInt32(lstSectionNumber.SelectedValue);
        double ticketQuantity = Convert.ToInt32(txtNumberOfTickets.Text);
    }

    catch (FormatException ex)
    {
        // put an error message.
    }
}

如果用户输入非数值,它将抛出异常。

尝试在
TryParse
中使用返回布尔值,如下所示:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int section = Convert.ToInt32(lstSectionNumber.SelectedValue);

    double ticketQuantity;
    if(!int.TryParse(txtNumberOfTickets.Text, out section))
    {
        //Some error handling here...like MessageBox.Show("Please enter a valid ticket quanity");
    }

    //Do you regular logic here...
}
旁注:为什么您的
ticketQuantity
是双精度的,但您正在转换为整数?还要注意,根据
1stSectionNumber
用作源的内容,可能会出现类似的转换错误。在处理用户输入时,最好使用Try-Catch,这样应用程序就不会崩溃。即使有错误的MessageBox通常也比桌面崩溃要好

 <tr>
        <td style="width:30%; text-align:right;">
              <asp:Label ID="lbltext" runat="server">Text Box</asp:Label>
        </td>
        <td style="width:30%; text-align:left;">
          <asp:TextBox ID="txtbox1" runat="server" 
                       ValidationGroup="Sample" 
                       Width="100%" >
          </asp:TextBox>
        </td>
        <td>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
                                            runat="server"
                                            ControlToValidate="txtbox1" 
                                            Display="Dynamic"
                                            ErrorMessage="Only Numbers"
                                            ForeColor="Red"
                                            ValidationExpression="^[0-9]*$">
           </asp:RegularExpressionValidator>

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                                        runat="server"
                                        ControlToValidate="txtbox1" 
                                        Display="Dynamic"
                                        ErrorMessage="Field cannot be left blank" 
                                        ForeColor="Red">
            </asp:RequiredFieldValidator>
         </td>
    </tr>
通过此操作,您将获得
值作为
0
作为
0.0

int section ;
int.TryParse(lstSectionNumber.SelectedValue,out section);
double ticketQuantity;
double.TryParse(txtNumberOfTickets.Text,out ticketQuantity);

如果出现错误,您将得到0或0.0而不会抛出错误。

请查看您是否能够帮助我将其添加到我的代码中?我试过好几种方法,但它总是给我一个错误。如果您可以将任何答案标记为已接受的答案@user3236592,那就太好了。这就是我的代码,但它仍然会给我一个错误。int段;int.TryParse(lstSectionNumber.SelectedValue.ToString(),out节);double ticketQuantity=转换为32(txtNumberOfTickets.Text);我没有意识到这是一个asp.net应用程序。显然,您希望在客户端进行一些验证,但仍然使用TryParse作为另一道防线来抵御错误输入。
 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int section;
        int.TryParse(ddl1.SelectedValue, out section);
        double ticketQuantity;
        double.TryParse(txtbox1.Text, out ticketQuantity);

    }
int section ;
int.TryParse(lstSectionNumber.SelectedValue,out section);
double ticketQuantity;
double.TryParse(txtNumberOfTickets.Text,out ticketQuantity);