系统.InvalidOperationException: ExecuteReader: CommandText属性未初

本文关键字:属性 ExecuteReader InvalidOperationException 系统 CommandText | 更新日期: 2023-09-27 18:15:01

当我关闭我的程序,我有这样的错误:系统。InvalidOperationException: ExecuteReader: CommandText属性未初始化有人能帮忙吗?下面是我的代码:

 try
        {
            conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=ADMIN-PC'SQLEXPRESS;AttachDbFilename=" +
                                    Environment.CurrentDirectory +
                                    @"'diplom.mdf;Integrated Security=True; User Instance = true";
            conn.Open();
        SqlCommand myCommand = conn.CreateCommand();
        string myComm = "";
        if (comboBox1.SelectedIndex == 0)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Директор'";
        if (comboBox1.SelectedIndex == 1)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Коммерческий директор'";
        if (comboBox1.SelectedIndex == 2)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Супервайзер'";
        if (comboBox1.SelectedIndex == 3)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Мерчендайзер'";
        if (comboBox1.SelectedIndex == 4)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Торговый агент'";
        if (comboBox1.SelectedIndex == 5)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Аналитик'";
        if (comboBox1.SelectedIndex == 6)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Главный бухгалтер'";
        if (comboBox1.SelectedIndex == 7)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Бухгалтер'";
        if (comboBox1.SelectedIndex == 8)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Начальник отдела'";
        if (comboBox1.SelectedIndex == 9)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Оператор'";
        if (comboBox1.SelectedIndex == 10)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Кассир'";
        if (comboBox1.SelectedIndex == 11)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Системный администратор'";
        if (comboBox1.SelectedIndex == 12)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Заведующий складом'";
        if (comboBox1.SelectedIndex == 13)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Кладовщик'";
        if (comboBox1.SelectedIndex == 14)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Грузчик'";
        if (comboBox1.SelectedIndex == 15)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Водитель'";
        if (comboBox1.SelectedIndex == 16)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Экспедитор'";
        myCommand.CommandText = myComm;
        SqlDataAdapter dataAdapter = new SqlDataAdapter();
        dataAdapter.SelectCommand = myCommand;
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds, "View2");
        dataGridView1.DataSource = ds.Tables["View2"].DefaultView;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        conn.Close();
    } 

系统.InvalidOperationException: ExecuteReader: CommandText属性未初

您的comboBox1.SelectedIndex不在0-16之间,因此留下myComm为空字符串。这就是为什么你会得到一个错误。还可以考虑使用switchdefault命令。

在执行命令之前,请进行检查。

if(!string.IsNullOrWhiteSpace(myComm))
{
    myCommand.CommandText = myComm;
    SqlDataAdapter dataAdapter = new SqlDataAdapter();
    dataAdapter.SelectCommand = myCommand;
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "View2");
    dataGridView1.DataSource = ds.Tables["View2"].DefaultView;
}

您的组合框的SelectedIndex有问题。有人已经说过,如果SelectedIndex不在0到16之间,则不初始化命令文本。我还想对你的代码进行优化,除了where条件外,查询文本总是相同的,所以你可以写

  string query = "SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, " + 
                 "position, brand, model, name_of_department FROM View2 " + 
                 "where position = @condition";
  string condition = string.Empty;
  switch(combobox1.SelectedIndex)
  {
       case 0:
          condition = "Директор";
          break;
       case 1:
          condition = "Коммерческий директор";
          break;
       ..... and so on 
}
if(!string.IsNullOrWhiteSpace(condition))
{
    myCommand.CommandText = query;
    myCommand.Parameters.AddWithValue("@condition", condition);
    SqlDataAdapter dataAdapter = new SqlDataAdapter();
    dataAdapter.SelectCommand = myCommand;
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "View2");
    dataGridView1.DataSource = ds.Tables["View2"].DefaultView;
}