C# 在Gridview中的按钮单击事件的文本框中输入nullable int后,如何进行验证?

C# 在Gridview中的按钮单击事件的文本框中输入nullable int后,如何进行验证?,c#,ado.net,C#,Ado.net,这是我的ADO.NET代码 protected void AddToCart_Click(object sender, EventArgs e) { GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent; int index = gvr.RowIndex; TextBox box1 = (TextBox)GridView1.Rows[index].Cells[9].FindC

这是我的ADO.NET代码

protected void AddToCart_Click(object sender, EventArgs e)
{           
    GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
    int index = gvr.RowIndex;
    TextBox box1 = (TextBox)GridView1.Rows[index].Cells[9].FindControl("TextBox1");
    int? Quantity = Convert.ToInt32(box1.Text);

    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
    string ToyId = row.Cells[0].Text;

    string CS1;
    CS1 = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Toy_WebForm; integrated security=SSPI";
    SqlConnection con1 = new SqlConnection(CS1);
    SqlCommand cmd1 = new SqlCommand("QuantityValidation", con1);
    cmd1.CommandType = System.Data.CommandType.StoredProcedure;

    cmd1.Parameters.AddWithValue("@ToyNo", ToyId);
    con1.Open();
    int ToyQuantity = Convert.ToInt32(cmd1.ExecuteScalar());
    con1.Close();

    if (Quantity <= ToyQuantity && Quantity > 0)
    {
        string CS2;
        CS2 = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Toy_WebForm; integrated security=SSPI";
        SqlConnection con2 = new SqlConnection(CS2);
        SqlCommand cmd2 = new SqlCommand("AddToCart", con2);
        cmd2.CommandType = System.Data.CommandType.StoredProcedure;

        cmd2.Parameters.AddWithValue("@ToyNo", ToyId);
        cmd2.Parameters.AddWithValue("@MemberNo", MemberId);
        cmd2.Parameters.AddWithValue("@TotalQuantity", Quantity);

        con2.Open();
        SqlParameter OutputParameter1 = new SqlParameter();
        OutputParameter1.ParameterName = "@CartNo";
        OutputParameter1.SqlDbType = System.Data.SqlDbType.UniqueIdentifier;
        OutputParameter1.Direction = System.Data.ParameterDirection.Output;
        cmd2.Parameters.Add(OutputParameter1);

        SqlParameter OutputParameter2 = new SqlParameter();
        OutputParameter2.ParameterName = "@TotalPrice";
        OutputParameter2.SqlDbType = System.Data.SqlDbType.Int;
        OutputParameter2.Direction = System.Data.ParameterDirection.Output;
        cmd2.Parameters.Add(OutputParameter2);

        cmd2.ExecuteNonQuery();

        con2.Close();

        string TotalPrice = OutputParameter2.Value.ToString();
        Label lbl1 = (Label)GridView1.Rows[index].Cells[10].FindControl("Label4");
        lbl1.Text = TotalPrice + " RM";

        Label3.Text = "Added to cart";
    }

    else if (Quantity>ToyQuantity)
    {
        Label3.Text = "Please enter within the stock limit";
    }

    else if (Quantity == 0 || Quantity == null)
    {
        Label3.Text = "Please add at least one quantity";
    }

}
否则,如果(数量==0)
在“添加到购物车”按钮单击事件中成功执行。 但是,
else如果(Quantity==null)
未在“添加到购物车”按钮单击事件上执行,并且在以下行显示错误:

int? Quantity = Convert.ToInt32(box1.Text);
上面这行的错误是

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


我可以得到一个示例代码来解决这个特殊问题吗?

让我先补充几点注意事项:由于
数量
是一个可为空的类型变量,因此可以使用
数量.HasValue
来检查变量是否为空。如果可空变量的
HasValue
属性为true,则表示该变量不是空的,并且包含一些值。查看错误消息时,
输入字符串的格式不正确。
这不是因为可为空或带有if条件,而是因为
框1中的输入值。Text
可能不是有效整数,因此
Convert.ToInt32
将引发异常

根据我在这里的观点,您需要一种更好的解析技术来代替nullable。因此int.TryParse将是您的正确选择。您可以像以下一样使用:

int Quantity;
if(int.TryParse(box1.Text, out Quantity))
   // continue with the value of Quantity
else
   box1.Text = "Invalid input";
int Quantity;
if(int.TryParse(box1.Text, out Quantity))
   // continue with the value of Quantity
else
   box1.Text = "Invalid input";