0

我有这个问题

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

突出显示的部分:

DisplayOrder(Convert.ToInt16(txtOrderNo.Text)); DisplayOrderDetails(Convert.ToInt16(txtOrderNo.Text));

我很难弄清楚错误是什么,你能帮我吗?非常感谢。

这是我的设置代码:

private void displayNavigate()
{
    DisplayOrder(Convert.ToInt16(txtOrderNo.Text));
    DisplayOrderDetails(Convert.ToInt16(txtOrderNo.Text));

    double dTotal = 0;

    try
    {
        for (int nRow = 0; nRow <= grdDetails.Rows.Count - 1; nRow++)
        {
            dTotal = dTotal + Convert.ToDouble((grdDetails.Rows[nRow].Cells["Amount"].Value.ToString()));
        }
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    lblTotal.Text = string.Format("{0:#,##00.00}", dTotal);
}

//================================================================================
//================================================================================


private void DisplayOrder(int nOrderNo)
{
    try
    {
        OpenConnection();

        SqlCommand cmdSelect = new SqlCommand();

        cmdSelect.Connection = cn;
        cmdSelect.CommandType = CommandType.Text;
        cmdSelect.Transaction = trnOrder;
        cmdSelect.CommandText = "SELECT " +
            "B.OrderNo, B.OrderDate, A.CustomerNo, " +
            "A.CustomerName, A.CustomerAddress, B.PurchaseOrderNo, B.AgentName, B.Status " +
            "FROM Customers AS A, Orders AS B " +
            "WHERE A.CustomerNo = B.CustomerNo " +
            "AND B.OrderNo ='" + nOrderNo + "'";

        SqlDataReader dr = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection);

        while (dr.Read())
        {
            txtOrderNo.Text = dr["OrderNo"].ToString();
            dtpOrderDate.Value = Convert.ToDateTime(dr["OrderDate"].ToString());
            txtCustomerNo.Text = dr["CustomerNo"].ToString();
            txtCustomerName.Text = dr["CustomerName"].ToString();
            txtCustomerAddress.Text = dr["CustomerAddress"].ToString();
            txtPONo.Text = dr["PurchaseOrderNo"].ToString();
            cboAgentName.Text = dr["AgentName"].ToString();
            txtOrderStatus.Text = dr["Status"].ToString();
        }
        dr.Close();
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

//================================================================================
//================================================================================

private void DisplayOrderDetails(int nOrderNo)
{
    OpenConnection();

    SqlCommand cmdSelect = new SqlCommand();
    cmdSelect.Connection = cn;
    cmdSelect.CommandType = CommandType.Text;
    cmdSelect.Transaction = trnOrder;
    cmdSelect.CommandText =
        "SELECT PackagingOutside, Quantity, Unit, ProductNo, ProductName, ProductSize, PackagingInside, " +
        "SellingDiscount, SellingPrice, Amount FROM OrderDetails WHERE OrderNo = '"
        + nOrderNo + "'";

    SqlDataAdapter daDetail = new SqlDataAdapter();
    daDetail.SelectCommand = cmdSelect;

    DataSet ds = new DataSet();
    daDetail.Fill(ds, "OrderDetails");

    grdDetails.DataSource = null;
    grdDetails.DataSource = ds.Tables["OrderDetails"];
}
4

3 回答 3

1

when you use Convert.ToInt16 you will get this exception if value does not consist of an optional sign followed by a sequence of digits (0 through 9)

Do a validation for inputs before proceed like below.

int orderNo;
if (int.TryParse(txtOrderNo.Text, out orderNo))
{
    DisplayOrder(orderNo);
    DisplayOrderDetails(orderNo);
}

Side Note :

don't share the SqlConnection create new instant when you need it and wrap it with using block like below

using (SqlConnection con = new SqlConnection(connectionString))
{

}

Use SQL Parameters

cmdSelect.CommandText = "SELECT * FROM Orders WHERE OrderNo = @OrderNo";
cmdSelect.Parameters.AddWithValue("@OrderNo", nOrderNo);
于 2013-09-17T06:04:40.403 回答
0

This means that the value in txtOrderNo.Text is not considered an integer. You will get this error if your textbox is empty.

Either check that the textbox contains data, or use the TryParse (http://msdn.microsoft.com/en-us/library/f02979c7.aspx) method

于 2013-09-17T06:05:15.337 回答
0

有两点需要考虑,

  1. 在文本框中输入的值应该是16位范围内的整数,如果它可以更大的值,那么你必须考虑去long,int32等。

  2. 验证文本框,使用TryParse()它将告诉您它是否输入了有效值。

于 2013-09-17T06:09:04.923 回答