如何在条件C#中比较int.MaxValue

Man*_*rio 0 c# asp.net

我想TextBox在ASP.NET中绑定它可以容纳的最大值int.MaxValue.以下代码引发错误.

using (SqlConnection scon = new SqlConnection(cs))
{
    SqlCommand cmd = new SqlCommand("spDelete", scon);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@id", TextBox6.Text);
    scon.Open();
    if (TextBox6.Text != null && TextBox6.Text <= int.MaxValue)
    {
        int del = cmd.ExecuteNonQuery();
        if (del == 0)
        {
            Label2.Visible = true;
            Label2.ForeColor = System.Drawing.Color.Red;
            Label2.Text = TextBox6.Text + " Record not found";
        }
        else
        {
            Label2.Visible = true;
            Label2.ForeColor = System.Drawing.Color.Red;
            Label2.Text = TextBox6.Text + " Deleted Successfully";
            LoadGV();
        }
    }
    else
    {
        Label2.Visible = true;
        Label2.ForeColor = System.Drawing.Color.Red;
        Label2.Text = TextBox6.Text + " Enter a valid value";
    }
}
Run Code Online (Sandbox Code Playgroud)

taf*_*fia 6

Textbox.Text是一个字符串,而不是int.我想你应该尝试将它转换为int之前:

int val;
if (Int32.TryParse(TextBox6.Text, out val) && val <= int.MaxValue) {
 // your stuff ...
}
Run Code Online (Sandbox Code Playgroud)

请注意,您不需要在if块之外设置连接