我有问题。我想使用这个 C# 在我的 mysqldatabase 上存储数据
private void btnSaveFilm_Click(object sender, EventArgs e)
{
try
{
MySqlConnection conn = new MySqlConnection(connection.mysqlconnectionbuilder());
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO film(judul,genre,asal,kondisi)"
+ "VALUES(@judul,@genre,@asal,@kondisi)";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@judul", textBoxJudul.Text);
cmd.Parameters.AddWithValue("@genre", category(comboBoxGenre.SelectedValue.ToString()).ToString());
cmd.Parameters.AddWithValue("@asal", asal(comboBoxAsal.SelectedValue.ToString()).ToString());
cmd.Parameters.AddWithValue("@kondisi", checkedStatus());
cmd.ExecuteNonQuery();
conn.Close();
}
catch(Exception exe)
{
Console.Write("Error on Save Film : " + exe.ToString() + "\n" +exe.Message);
}
}
但它显示错误 System.NullReferenceException: Object reference not set to an instance of an object。
第 40 行的错误:
cmd.Parameters.AddWithValue("@genre",kategori(comboBoxGenre.SelectedValue.ToString()).ToString());
如何解决?