Reputation: 31
i wrote everything correctly i think... but i keep getting this error , saying the input string is not in a correct format
so first i take in values from the user then convert in into int then calculate then return it back to string
private void btnenroll_Click(object sender, RoutedEventArgs e)
{
string things;
string morethings;
try
{
id = Convert.ToInt32(txtempty.Text) + 1;
con.Open();
things = "INSERT INTO vikoba.members(id_no , names_) VALUES (@id) , (@name)";
cmd = new MySqlCommand(things, con);
cmd.Parameters.Add(new MySqlParameter( "@id", MySqlDbType.Int32,4));
cmd.Parameters["@id"].Value = id.ToString();
cmd.Parameters.Add(new MySqlParameter("@name", MySqlDbType.VarChar,20));
cmd.Parameters["@name"].Value = txtname.Text;
cmd.ExecuteNonQuery();
morethings = "INSERT INTO vikoba.mpoko(id_no , jumpla_mpoko , mkopo , riba , bima) VALUES (@id) , (@jumla) , (@mkop) , (@riba) ,(@bima) ";
cmd = new MySqlCommand(morethings, con);
cmd.Parameters.Add(new MySqlParameter("@id", MySqlDbType.Int32));
cmd.Parameters["@id"].Value = id.ToString();
cmd.Parameters.Add(new MySqlParameter ("@jumla", MySqlDbType.Int32));
cmd.Parameters["@jumla"].Value = txtjumla.Text;
cmd.Parameters.Add(new MySqlParameter("@mkop",MySqlDbType.Int32));
cmd.Parameters["@mkop"].Value = txtloan.Text;
cmd.Parameters.Add(new MySqlParameter ("@riba", MySqlDbType.Int32));
cmd.Parameters["@riba"].Value = txtriba.Text;
cmd.Parameters.Add(new MySqlParameter("@bima", MySqlDbType.Int32));
cmd.Parameters["@bima"].Value = txtbima.Text;
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
Upvotes: 0
Views: 1013
Reputation: 314
The first answer is correct but you have a bit of a flaw. On an exception your connection will be left open. It will clean up "someday" when garbage collection kicks in. Your command object is derived from Component which also implements IDisposable.
Always wrap classes derived from IDisposable with the using keyword.
using (MySqlConnection conn = new MySqlConnection("connection text here"))
{
conn.Open();
things = "INSERT INTO vikoba.members(id_no , names_) VALUES (@id, @name)";
using (MySqlCommand cmd = new MySqlCommand(things, con))
{
/* body of code here */
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
}
Upvotes: 0
Reputation: 216293
You have a wrong syntax for your sql queries
things = "INSERT INTO vikoba.members(id_no , names_) VALUES (@id, @name)";
cmd = new MySqlCommand(things, con);
cmd.Parameters.Add(new MySqlParameter( "@id", MySqlDbType.Int32,4));
cmd.Parameters["@id"].Value = id;
cmd.Parameters.Add(new MySqlParameter("@name", MySqlDbType.VarChar,20));
cmd.Parameters["@name"].Value = txtname.Text;
cmd.ExecuteNonQuery();
Removed the VALUES(@ID), (@name)
and do not try to convert the id variable to a string when passing it as parameter value. The same things happen in the second query
morethings = "INSERT INTO vikoba.mpoko(id_no , jumpla_mpoko , mkopo , riba , bima) " +
"VALUES (@id, @jumla, @mkop, @riba, @bima)";
Of course the value in the textbox should be an integer number otherwise everything fails at that line. To be sure change that code using TryParse
int result
if(!Int32.TryParse(txtempty.Text, out result)
{
MessageBox.Show("Not a valid integer number");
return;
}
id = result + 1;
....
Looking again to the second part of your query, there are other potential problems with the other textbox used as input for the parameters. Every parameters is expected to be an Int32 but you pass strings to the parameter values and you should absolutely sure that they are numbers convertible in Int32. Looking at all you would change to this code
private void btnenroll_Click(object sender, RoutedEventArgs e)
{
string things;
string morethings;
try
{
int id;
if(!Int32.TryParse(txtempty.Text, out id)
{
MessageBox.Show("Not a valid ID");
return;
}
int jumla;
if(!Int32.TryParse(txtjumla.Text, out jumla)
{
MessageBox.Show("Not a valid JUMLA");
return;
}
int mkop;
if(!Int32.TryParse(txtloan.Text, out mkop)
{
MessageBox.Show("Not a valid MKOP");
return;
}
....
.... and so on for the other integer required by the second query
....
id += 1;
con.Open();
things = "INSERT INTO vikoba.members(id_no , names_) VALUES (@id ,@name)";
cmd = new MySqlCommand(things, con);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.ExecuteNonQuery();
morethings = "INSERT INTO vikoba.mpoko(id_no , jumpla_mpoko , mkopo , riba , bima) " +
"VALUES (@id, @jumla, @mkop, @riba, @bima) ";
cmd = new MySqlCommand(morethings, con);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@jumla", jumla);
cmd.Parameters.AddWithValue("@mkop", mkop);
.... other parameters ...
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
Upvotes: 1