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();
7
  • Make sure there are no white spaces, and maybe consider using TryParse(). Commented May 23, 2016 at 18:57
  • What are the values of txtNoRooms.Text and txtNoNights.Text? Commented May 23, 2016 at 19:00
  • Using the Tryparse is a good option to prevent other characters, other than number, from being added to the TextBoxes. Also, what data type is NoOfRooms on the Sql Server table? Commented May 23, 2016 at 19:06
  • @DourHighArch txtNoRoom.text is any whole number for example 5(int) same with txtNoNights.text Commented May 23, 2016 at 19:07
  • @Auguste NoOfRooms is int on the database Commented May 23, 2016 at 19:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

same error appeared so it was false (with ur line of code)
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.
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

1 Comment

@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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.