2017-02-28 53 views
0

这是我的ADO.NET代码如何在GridView中的按钮单击事件的文本框中输入可空int后验证?

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"; 
    } 

} 

所有if和else if条件,成功地工作添加到购物车按钮点击事件。 但是,如果你看看过去的,如果条件:

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

else if(Quantity == 0)正在执行成功添加到购物车按钮点击事件。 然而,else if (Quantity == null)没有被添加到购物车按钮点击事件执行,它显示在下面的行错误:

int? Quantity = Convert.ToInt32(box1.Text); 

上方直线的错误说

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

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

回答

0

让我先添加几个备注:由于Quantity是一个可为空的类型变量,因此您可以使用Quantity.HasValue来检查变量是否为空。如果可空变量的HasValue属性为真,则表示该变量不为空并包含某个值。在查看错误消息时,Input string was not in a correct format.不是因为可为空或if条件,这是因为box1.Text中的输入值可能不是有效整数,因此Convert.ToInt32会引发异常。

根据我的观点,在这里你需要一个更好的解析技术,而不是空的。所以int.TryParse将是您的正确选择。你可以像下面这样使用:

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