关闭我的程序时,我有这样的错误: 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();
}