投射项目为空

时间:2018-01-26 19:24:00

标签: c# winforms

如何修复以下代码中的演员项null错误?当从ListBox我选择一个项目,然后单击确认按钮,然后打开另一个窗口表单,如果我确认我的操作,从Sql中的表中减去该项目时,就会发生这种情况。然而,之后我得到“对象引用未设置为对象的实例”。以下代码仅用于根据所选项目更改图像。

private void button1_Click(object sender,EventArgs e)

    {
        Confirm_rental c = new Confirm_rental("Please confirm?");
        DialogResult dr = c.ShowDialog();
        if (dr == DialogResult.Yes)
            try
            {
                Registering_rental_in_DB();
                bikeT.Clear();
                adaptb.Fill(bikeT);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        if (dr == DialogResult.No)
        {
            MessageBox.Show("Selection wasn't registered");
            bikes.ClearSelected();
            foreach (int i in bikes.SelectedIndices)
                bikes.SetSelected(i, false);
        }
    }

private void bikes_SelectedIndexChanged(object sender, EventArgs e)
{
  DataRowView castedItem = bikes.SelectedItem as DataRowView;
  string name = castedItem["model"].ToString();

  if (name == "Bike 1")
  {
    pictureBox1.Image = Image.FromFile("image path");
  }
  else if (name == "Bike 2")
  {
    pictureBox1.Image = Image.FromFile("image path");
  }
  else if (name == "Bike 3")
  {
    pictureBox1.Image = Image.FromFile("image path");
  }

0 个答案:

没有答案