@Maxmil

Не работает поиск по data, но по number работает, почему?

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            int index = comboBox1.SelectedIndex;
            switch (index)
            {
                case 0:
                    search_number();
                    break;
                case 1:
                    search_data();
                    break;
            }
        }

        private void search_number()
        {
            try
            {
                if (textBox8.Text != string.Empty)
                {
                    DataSet dataSet = new DataSet();
                    BindingSource  binding= new BindingSource();

                    SqlDataAdapter poi = new SqlDataAdapter("SELECT number,name,city, data FROM people O WHERE  O.number LIKE N'%" + this.textBox8.Text + "%'", conpo);
                    poi.Fill(dataSet, "people");
                    binding = new BindingSource(dataSet, "people");
                    dataGridView1.DataSource = binding;
                }
                else
                    MessageBox.Show("Enter what to find", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (SqlException sqlExcept)
            { MessageBox.Show(sqlExcept.Message); }
        }

        private void search_data()
        { try
            {
                if (textBox8.Text != string.Empty)
                {
                    DataSet dataS = new DataSet();
                    BindingSource  binding= new BindingSource();

                    SqlDataAdapter poi = new SqlDataAdapter("SELECT number,name,city, data FROM people O WHERE  O.data LIKE N'%" + this.textBox8.Text + "%'", conpo);
                    poi.Fill(dataS, "people");
                    binding = new BindingSource(dataS, "people");
                    dataGridView1.DataSource = binding;
                }
                else
                    MessageBox.Show("Enter what to find", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (SqlException sqlExcept)
            { MessageBox.Show(sqlExcept.Message); }
        }


        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "number")
            {
                SqlConnection po = new SqlConnection("Data Source=max;Initial Catalog=lr;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("SELECT number,name,city, data FROM people WHERE number like '" + textBox8.Text + "%' ", po);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            else if (comboBox1.Text == "data")
            {
                SqlConnection po = new SqlConnection("Data Source=max;Initial Catalog=lr;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("SELECT number,name,city, data FROM people WHERE data like '" + textBox8.Text + "%'", po);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }
  • Вопрос задан
  • 232 просмотра
Решения вопроса 1
w1ld
@w1ld
Программирую
Там в `textBox8_TextChanged` пропущено в начале `%`. Д.б. так:

private void textBox8_TextChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "number")
            {
                SqlConnection po = new SqlConnection("Data Source=max;Initial Catalog=lr;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("SELECT number,name,city, data FROM people WHERE number like '%" + textBox8.Text + "%' ", po);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            else if (comboBox1.Text == "data")
            {
                SqlConnection po = new SqlConnection("Data Source=max;Initial Catalog=lr;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("SELECT number,name,city, data FROM people WHERE data like '%" + textBox8.Text + "%'", po);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }


ЗЫ. Надеюсь это на 1-2 дня код, а не в продакшн. Много зависимостей и др.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы