I'm working on a project which requires a Register option. I have already created a form that a user can fill out and it successfully saves to the database, if every text box has been filled out. I need to have some of them be optional.
private void button1_Click(object sender, EventArgs e)
{
Registercar(textBox1.Text, textBox2.Text, Convert.ToInt32(textBox3.Text));
}
private static void Registercar(string make, string model, int year)
{
var con = GetConnection();
try
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO user_car (ID,make,model,year) VALUE (@ID, @make, @model, @year)";
cmd.Prepare();
cmd.Parameters.AddWithValue("@ID", 0);
cmd.Parameters.AddWithValue("@make", make);
cmd.Parameters.AddWithValue("@model", model);
cmd.Parameters.AddWithValue("@year", year);
cmd.ExecuteNonQuery();
if (con !=null) { con.Close(); };
MessageBox.Show("Car successfully registered");
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex));
}
finally
{
con.Close();
}
}
The code above is the part of the register, where the user has the option to register a car that will be tied to their account. If they choose the add a car, they need to input the Make and the Model, while the Year is optional. If they input all the information, it successfully saves the information into the database.
However, If I try to leave the Year option blank, I get the following error: "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format."
This tells me that I am most likely not allowed to leave it blank. I tried to figure out different options where I could leave it blank, and still save the other information into the database. The first option I tried came from this post: How can we declare Optional Parameters in C#.net? I tried to make the year optional, by inputting a default value to my Year parameter:
private static void Registercar(string make, string model, int year = 0)
However I still get the same error as before. The next option I tried came from this post: How can you use optional parameters in C#? The first answer was the same as the previous one, set the parameter to a set value. Another answer further down suggested to use "System.Runtime.InteropServices" and set the parameter to [Optional]. I tried, but I get the same error as before. I figured it had to do with the fact that I try to convert and empty string to an int. A quick search led me to this post: Convert string to integer Here I am told I can try to use int.Parse, so I set it up like this
private void button1_Click(object sender, EventArgs e)
{
int year = int.Parse(textBox3.Text.Trim());
Registercar(textBox1.Text, textBox2.Text, year);
}
Same error as before. Next I tried the "Int32.TryParse()" which checks if the string contains an int, if not, output an integer value
private void button1_Click(object sender, EventArgs e)
{
int year;
string str = textBox3.Text.Trim();
Registercar(textBox1.Text, textBox2.Text, Int32.TryParse(str, out year);
}
Now it tells me I can't convert from 'bool' to 'int'
At this point I have pretty much almost given up.
The thing that works currently, is that if every box is filled out, it successfully saves the information to the database. However, I need some of the text boxes to be optional, and I can't get that to work.
I'm sorry for the long post. I hope someone is able to help or come with a suggestion :)