I’m new to creating applications using Visual Studio 2010 and MySQL. I am creating an application that adds the information to the database. But then when I click the add button, there is an error message which says Input string was not in a correct format. But I enter letters to the textbox and I used VarChar as the DataType. Now, I can’t figure out what’s the problem.
Code:
private void buttonaddcompany_Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" + "DATABASE=payroll;" + "UID=root;" + "PASSWORD=admin;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
command.Connection = connection;
using (MySqlConnection conn = new MySqlConnection(MyConString))
{
connection.Open();
using (MySqlCommand com = connection.CreateCommand())
{
command.CommandText = "insert into company(company_name, company_other_names, company_office_number, company_office_building, company_office_street, company_office_village, company_municipality_name, company_municipality_zipcode, company_province_name, company_province_zipcode, company_country_name, company_country_zipcode) values(?company_name, ?company_other_names, ?company_office_number, ?company_office_building, ?company_office_street, ?company_office_village, ?company_municipality_name, ?company_municipality_zipcode, ?company_province_name, ?company_province_zipcode, ?company_country_name, ?company_country_zipcode)";
command.Parameters.Add(new MySqlParameter("?company_name", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_other_names", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_office_number", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_office_building", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_office_street", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_office_village", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_municipality_name", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_municipality_zipcode", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_province_name", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_province_zipcode", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_country_name", MySqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("?company_country_zipcode", MySqlDbType.VarChar));
command.Parameters["?company_name"].Value = addcompname.Text;
command.Parameters["?company_other_names"].Value = addcompothername.Text;
command.Parameters["?company_office_number"].Value = addoffnumber.Text;
command.Parameters["?company_office_building"].Value = addoffbuilding.Text;
command.Parameters["?company_office_street"].Value = addoffstreet.Text;
command.Parameters["?company_office_village"].Value = addoffvillage.Text;
command.Parameters["?company_municipality_name"].Value = addoffmunname.Text;
command.Parameters["?company_municipality_zipcode"].Value = addoffmunzipcode.Text;
command.Parameters["?company_province_name"].Value = addoffprovname.Text;
command.Parameters["?company_province_zipcode"].Value = addoffprovzipcode.Text;
command.Parameters["?company_country_name"].Value = addoffcountryname.Text;
command.Parameters["?company_country_zipcode"].Value = addoffcountryzipcode.Text;
command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
}
}
}
Screenshot:

Not an answer, but maybe a common way to fight such problems in a future. If I encounter such problems, I simplify it to minimal form. In your case this would be some thing like:
this allows to narrow places there some typos can drive you crazy. Stick with simplified form until problem is resolved, so you can be sure that code logic is correct. Then return to full code version and see if it working. If not, then probably it’s just typos.