我在组合框值处有一个空引用异常。我允许在数据库中为空。但它不起作用。我应该怎么办?我在评论中提到了异常区域:这是我的源代码。
private void button1_Click(object sender, EventArgs e)
{
con.Open();
cmd.CommandText = "insert into mytable (userid, password, gender) values (@userid, @password, @gender)";
cmd.Parameters.AddWithValue("@userid", textBox1.Text);
cmd.Parameters.AddWithValue("@password", textBox2.Text);
cmd.Parameters.AddWithValue("@gender", comboBox1.SelectedItem.ToString());//Here is exception line (NullReference Exception)
cmd.ExecuteNonQuery();
cmd.Clone();
con.Close();
MessageBox.Show("Record Added");
}
private void Form2_Load(object sender, EventArgs e)
{
cmd.Connection = con;
}