0

The error comes around "NoOfRooms" there is also another textbox called "NoOfDays" which is the same. If 1 of the textbox has value the other will pop the error below however if both them have a value the program works fine.. I don't know how to make 1 work only without the error.

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.

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();
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
aalloo
  • 27
  • 4

2 Answers2

1

I searched on google the 'System.FormatException' and this link seemed useful this link

Here it says that "You can't convert a null value to a meaningful value through Convert. Your best option is to check for null first and then assign a value of 0 (or whatever) to the result (whereever Convert was sending its results too)."

You can check of null or you can try this:

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

Here result will be true if the conversion was finalized successfully and false otherwise. And the same for the double value. You can then use the Boolean values in order to determine if the conversion finished successfully and take some actions accordingly.

Guest
  • 61
  • 5
  • same error appeared so it was false (with ur line of code) – aalloo May 23 '16 at 19:59
  • I proposed this solution so you can know when the conversion finished correctly. You can add some verification to know when it is false and not use the incorrect converted values. – Guest May 25 '16 at 08:12
0

Given that your table was pretty simple and it seemed odd that it didn't work I thought I'd try it out myself. Below is what I came up with:

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

Consider using a Stored Procedure

Instead of writing the INSERT statement directly, I tend to use a SP to insert/update

Alex W
  • 475
  • 5
  • 7
  • @aalloo glad I could help. Usually when I'm trying to run a sql statement like a select/insert/update/delete I run it in sql server first to make sure it works properly. Then I just copy and paste it into the code, then make the necessary adjustments – Alex W May 24 '16 at 23:12