-1

关闭我的程序时,我有这样的错误: System.InvalidOperationException: ExecuteReader: CommandText property has been not initialized 有人可以帮忙吗?这是我的代码:

 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();
    } 
4

2 回答 2

2

comboBox1.SelectedIndex不在之间,0-16因此保留myComm为空字符串。这就是您收到错误的原因。还可以考虑使用switch命令default

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

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;
}
于 2013-05-23T07:26:03.143 回答
1

组合框的 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;
}
于 2013-05-23T07:36:52.447 回答