【问题标题】:Input string was not in a correct format. C#输入字符串的格式不正确。 C#
【发布时间】:2015-07-14 05:46:24
【问题描述】:

当预算显示值小于成本显示提交按钮将被隐藏的金额时,我正在执行验证。但是我收到一个错误“输入字符串的格式不正确。”

下面是我的代码


void GetInfo(int ID)
{
    var cost = decimal.Parse(txtCost.Text);
    var budget = decimal.Parse(txtBudget.Text);

        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT ProjectID, ClientID, Name, Description, Location, DateStarted, " +
            "DateEstFinished, Budget - ActualCost AS Total, ActualCost, Status FROM Projects WHERE ProjectID=@ProjectID";
        cmd.Parameters.AddWithValue("@ProjectID", ID);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                ltID.Text = dr["ProjectID"].ToString();
                ltName.Text = dr["Name"].ToString();
                ddlTypes.Text = dr["ClientID"].ToString();
                txtName.Text = dr["Name"].ToString();
                txtDescription.Text = dr["Description"].ToString();
                txtLocation.Text = dr["Location"].ToString();
                DateTime dateStarted = DateTime.Parse(dr["DateStarted"].ToString());
                txtDateStarted.Text = dateStarted.ToString("MMMM dd, yyyy");
                txtStart.Attributes.Add("min", dateStarted.ToString("yyyy-MM-dd"));
                DateTime dateFinish = DateTime.Parse(dr["DateEstFinished"].ToString());
                txtDateFinish.Text = dateStarted.ToString("MMMM dd, yyyy");

                DateTime start = DateTime.Now;
                bool validStart = DateTime.TryParse(txtStart.Text, out start);

                if (validStart)
                    txtFinish.Attributes.Add("min", start.AddDays(1).ToString("yyyy-MM-dd"));
                txtBudget.Text = dr["Total"].ToString();
                txtCost.Text = dr["ActualCost"].ToString();
            }
            con.Close();
            if (budget >= cost)
            {
                btnSubmit.Visible = false;
            }
        }
        else
        {
            con.Close();
            Response.Redirect("~/Projects/Default.aspx");
        }

}

【问题讨论】:

  • 在哪一行,给出错误?
  • txtBudget.Text 的值是多少?
  • 还有txtCost.Text
  • Use the Debugger,卢克!
  • 您的问题很可能出在DateTime dateStarted = DateTime.Parse(dr["DateStarted"].ToString());。您的日期是否存储为字符串?如果是这样为什么?它们应存储为日期值。空日期是作为空字符串还是 null 返回?如果问题不在这里,它很可能在日期格式字符串之一中。你没有提供足够的信息。您应该能够通过行号判断哪一行受到影响,或者放置断点并调试,观察值。

标签: c# asp.net validation


【解决方案1】:

我解决了这个问题:

if (Decimal.Parse(txtBudget.Text) >= Decimal.Parse(txtCost.Text))
{
    btnSubmit.Visible = true;
}
else
{
    btnSubmit.Visible = false;
}

然后声明一个 var decimal

【讨论】:

  • 如果用户在txtBudget字段中输入“bla-bla-bla”怎么办?
  • 同意 Dmitry ...您需要验证条目...而且...btnSubmit.Visible = Decimal.Parse(txtBudget.Text) >= Decimal.Parse(txtCost.Text)
【解决方案2】:

类似的东西:

Decimal budget;
Decimal cost;

if (!Decimal.TryParse(txtBudget.Text, out budget)) {
  btnSubmit.Visible = false; 
  //TODO: probably you have to show message that txtBudget has incorrect value
}
else if (!Decimal.TryParse(txtCost.Text, out cost)) {
  btnSubmit.Visible = false; 
  //TODO: probably you have to show message that txtCost has incorrect value
}
else {
  //TODO: you may find useful to check if cost < 0 or/and budget < 0 etc.
  btnSubmit.Visible = budget >= cost;  
}

【讨论】:

    【解决方案3】:

    您的问题将出现在DateTime 转换中,因为如果它会得到 blankNull 值然后它会给出上述错误所以 为您拥有的所有代码执行以下提供的代码示例 使用datetime

    if(dr["DateStarted"].ToString() != "")
    {
       txtDateStarted.Text = Convert.ToDateTime(dr["DateStarted"]).ToString("MMMM dd, yyyy");
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多