I have recently adjusted my code to avoid getting SQL injections and got helped with adding parameters but now the code is semi-foreign to me and was wondering what object reference is required for MySqlCommand.Parameters
MySqlParameter parameter = new MySqlParameter();
parameter.ParameterName = "@userid";
parameter.MySqlDbType = MySqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = this.userid_txt.Text;
parameter.Value = this.email_txt.Text;
parameter.Value = this.passone_txt.Text;
parameter.Value = this.passtwo_txt.Text;
parameter.Value = this.lastname_txt.Text;
parameter.Value = this.firstname_txt.Text;
string Query = "insert into userdatabase.users (userid, email, passone, passtwo, lastname, firstname) values(@userid,@email,@passone,@passtwo,@lastname_txt,@firstname)";
MySqlCommand.Parameters.AddWithValue("@userid", this.userid_txt.Text);
MySqlCommand.Parameters.AddWithValue("@email", this.email_txt.Text);
MySqlCommand.Parameters.AddWithValue("@passone", this.passone_txt.Text);
MySqlCommand.Parameters.AddWithValue("@passtwo", this.passtwo_txt.Text);
MySqlCommand.Parameters.AddWithValue("@lastname", this.lastname_txt.Text);
MySqlCommand.Parameters.AddWithValue("@firstname", this.firstname_txt.Text);
MySqlConnection conDataBase = new MySqlConnection();
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
MySqlCommand cmd = new MySqlCommand(Query, conDataBase);
cmd.ExecuteNonQuery()
The errors I have state
'MySql.Data.MySqlClient.MySqlDbType' does not contain a defintion for 'NVarChar'
and the main
An object refrence is required for non-static field, method or property 'MySql.Data.MySqlClient.MySqlCommand.Parameters.get'
I am relatively new to using MySql so any help is appreciated
EDIT: New code which causes a fatal error once I click register which connects to my database
MySqlCommand cmdDataBase = new MySqlCommand();
MySqlParameter parameter = new MySqlParameter();
string constring = "datasource=127.0.0.1;port=3306;username=root;password=welcome";
string Query = "insert into userdatabase.users (userid, email, passone, passtwo, lastname, firstname) values(@userid,@email,@passone,@passtwo,@lastname_txt,@firstname)";
cmdDataBase.Parameters.Add("@userid", MySqlDbType.VarChar).Value = userid_txt.Text;
cmdDataBase.Parameters.Add("@email", MySqlDbType.VarChar).Value = email_txt.Text;
cmdDataBase.Parameters.Add("@passone", MySqlDbType.VarChar).Value = passone_txt.Text;
cmdDataBase.Parameters.Add("@passtwo", MySqlDbType.VarChar).Value = passtwo_txt.Text;
cmdDataBase.Parameters.Add("@lastname", MySqlDbType.VarChar).Value = lastname_txt.Text;
cmdDataBase.Parameters.Add("@firstname", MySqlDbType.VarChar).Value = firstname_txt.Text;
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmd = new MySqlCommand(Query, conDataBase);
try {
conDataBase.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Welcome to iDSTEM!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
You need the command itself - you're building it later:
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
Just move that higher, and then use:
cmdDataBase.Parameters.AddWithValue(...);
It's not clear why you're creating the parameter
variable at all, as you're never using it.
I would actually not use AddWithValue
, but instead use:
cmdDataBase.Parameters.Add("@userid", MySqlDbType.VarChar).Value = userid_txt.Text;
// etc
MySQL doesn't have a type of NVarChar
, so MySqlDbType
doesn't either, hence your error - I strongly suspect you just want VarChar
. You don't need to specify the direction - that's implicitly "input" by default.
Additionally, you should be using using
statements for your connection and command:
using (var connection = new MySqlConnection(...))
{
using (var command = new MySqlCommand(sql, connection))
{
...
}
}
See more on this question at Stackoverflow