//What's wrong in code !!!protected void Button1_Click(object sender, EventArgs e)
{
MySqlConnection conx;
conx = new MySqlConnection("server=localhost; database=pt; user id=root;");
MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (@A,@AB)", conx);
SimpanAdmin.Parameters.Add("@A", MySqlDbType.VarChar,50);
SimpanAdmin.Parameters.Add("@AB", MySqlDbType.VarChar,45);
SimpanAdmin.Parameters["@A"].Value = TextBox1.Text;
SimpanAdmin.Parameters["@AB"].Value = TextBox2.Text;
chy.Text = "Proses Menyimpan Berhasil...!!!";
try
{
SimpanAdmin.Connection.Open();
SimpanAdmin.ExecuteNonQuery();
SimpanAdmin.Connection.Close();
}
catch
{
chy.Text = "Proses Menyimpan Gagal...!!!";
}
}
// Can you help me for wrong code...i am finish for build but cannot save in MySql database :confused::confused:
解决方案You try the following code ,It may be helpful
protected void Button1_Click(object sender, EventArgs e)
{
MySqlConnection conx;
conx = new MySqlConnection("server=localhost; database=pt; user id=root;");
try
{
MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (@A,@AB)", conx);
SimpanAdmin.Connection.Open();
SimpanAdmin.Parameters.Add("@A", MySqlDbType.VarChar, 50).Value = TextBox1.Text;
SimpanAdmin.Parameters.Add("@AB", MySqlDbType.VarChar, 45).Value = TextBox2.Text;
chy.Text = "Proses Menyimpan Berhasil...!!!";
SimpanAdmin.ExecuteNonQuery();
}
catch
{
chy.Text = "Proses Menyimpan Gagal...!!!";
}
finally { SimpanAdmin.Connection.Close();
}
}
Best Regards,
Theingi Win.
Hello,
I have the same problem, my solution was to change @ to ? like:
protected void Button1_Click(object sender, EventArgs e)
{
MySqlConnection conx;
conx = new MySqlConnection("server=localhost; database=pt; user id=root;");
MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (?A,?AB)", conx);
SimpanAdmin.Parameters.Add("?A", MySqlDbType.VarChar,50).Value = TextBox1.Text;
SimpanAdmin.Parameters.Add("?AB", MySqlDbType.VarChar,45).Value = TextBox2.Text;
chy.Text = "Proses Menyimpan Berhasil...!!!";
try
{
SimpanAdmin.Connection.Open();
SimpanAdmin.ExecuteNonQuery();
SimpanAdmin.Connection.Close();
}
catch
{
chy.Text = "Proses Menyimpan Gagal...!!!";
}
}
HTH
Old question but from here:
http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html[^]
Note. Prior versions of the provider used the '@' symbol to mark parameters in SQL. This is incompatible with MySQL user variables, so the provider now uses the '?' symbol to locate parameters in SQL. To support older code, you can set 'old syntax=yes' on your connection string. If you do this, please be aware that an exception will not be throw if you fail to define a parameter that you intended to use in your SQL.