【问题标题】:"Input string was not in a correct format" when parsing numbers [duplicate]解析数字时“输入字符串的格式不正确”[重复]
【发布时间】:2016-09-20 18:37:00
【问题描述】:

错误来自“NoOfRooms”,还有另一个名为“NoOfDays”的文本框是相同的。如果文本框的 1 具有值,则另一个将弹出下面的错误,但是如果它们都有值,则程序可以正常工作。我不知道如何使 1 仅在没有错误的情况下工作。

mscorlib.dll 中出现“System.FormatException”类型的未处理异常
附加信息:输入字符串的格式不正确。

int NoOfRooms = 0;
double NoOfNights = 0;

NoOfRooms = Convert.ToInt32(txtNoRooms.Text);
NoOfNights = Convert.ToDouble(txtNoNights.Text);
totalCostHotel = (totalCostHotel * NoOfRooms) * NoOfNights;


{
    SqlCommand command = new SqlCommand("INSERT INTO [HotelData] (RoomType, NoOfRooms, NoOfNights, totalCostHotel)"+
        "VALUES (@RoomType, @NoOfRooms, @NoOfNights, @totalCostHotel)", connection);

    connection.Open();
    command.Parameters.AddWithValue("@RoomType", RoomType);
    command.Parameters.AddWithValue("@NoOfRooms", NoOfRooms);
    command.ExecuteNonQuery();
    connection.Close();

【问题讨论】:

  • 确保没有空格,也许可以考虑使用TryParse()
  • txtNoRooms.TexttxtNoNights.Text的值是多少?
  • 使用 Tryparse 是防止将数字以外的其他字符添加到文本框的好选择。还有,Sql Server 表上的 NoOfRooms 是什么数据类型?
  • @DourHighArch txtNoRoom.text 是任何整数,例如 5(int) 与 txtNoNights.text 相同
  • @Auguste NoOfRooms 在数据库中是整数

标签: c# formatexception


【解决方案1】:

我在谷歌上搜索了“System.FormatException”,这个链接似乎很有用 this link

这里说“您不能通过 Convert 将 null 值转换为有意义的值。最好的选择是先检查 null,然后将值 0(或其他值)分配给结果(无论 Convert 是也发送结果)。”

你可以检查 null 或者你可以试试这个:

  int NoOfRooms ;
  bool result = Int32.TryParse(txtNoRooms.Text, out NoOfRooms);

如果转换成功完成,则此处的结果将为 true,否则为 false。对于 double 值也是如此。然后,您可以使用布尔值来确定转换是否成功完成并采取相应的措施。

【讨论】:

  • 出现了同样的错误,所以它是错误的(使用你的代码行)
  • 我提出了这个解决方案,以便您知道转换何时正确完成。您可以添加一些验证以了解何时为 false 并且不要使用错误的转换值。
【解决方案2】:

鉴于您的表非常简单,而且它不起作用似乎很奇怪,我想我会自己尝试一下。以下是我想出的:

var roomCost = 100.0; // whatever the room cost based on the room type
// You should probably use try parse
var RoomType = string.IsNullOrEmpty(txtRoomType.Text) ? 1 : Convert.ToInt32(txtRoomType.Text.Trim());
var NoOfRooms = string.IsNullOrEmpty(txtNoOfRooms.Text) ? 1 : Convert.ToInt32(txtNoOfRooms.Text.Trim());
var NoOfNights = string.IsNullOrEmpty(txtNoOfNights.Text) ? 1 : Convert.ToInt32(txtNoOfNights.Text.Trim());
var totalCostHotel = (roomCost * NoOfRooms) * NoOfNights;
// you will need to change the connection string to what you need
var connectionString = "server=localhost;database=testdb;Integrated Security=true;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    var queryString = new StringBuilder();
    queryString.AppendLine("INSERT INTO [dbo].[HotelData]");
    queryString.AppendLine("([RoomType],[NoOfRooms],[NoOfNights],[totalCostHotel])");
    queryString.AppendLine("VALUES (@RoomType, @NoOfRooms, @NoOfNights, @totalCostHotel)");
    try
    {
        SqlCommand command = new SqlCommand(queryString.ToString(), connection);
        command.Connection.Open();
        command.Parameters.AddWithValue("@RoomType", RoomType);
        command.Parameters.AddWithValue("@NoOfRooms", NoOfRooms);
        command.Parameters.AddWithValue("@NoOfNights", NoOfNights);
        command.Parameters.AddWithValue("@totalCostHotel", totalCostHotel);
        command.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

考虑使用存储过程

我倾向于使用SP来插入/更新,而不是直接编写INSERT语句

【讨论】:

  • @aalloo 很高兴我能帮上忙。通常,当我尝试运行像 select/insert/update/delete 这样的 sql 语句时,我首先在 sql server 中运行它以确保它正常工作。然后我只是将其复制并粘贴到代码中,然后进行必要的调整
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多