1

Code below is working properly and view all matches by search in column.

string sql = "SELECT car, model, year FROM store WHERE" + column + "LIKE " + search + "'"; 

Now adding parameters in query. Not working. It doesn't display search in column. Only display all rows in column, if search column of column ( 1 = 1)

    public int SearchCar(MainStore searchCars)
    {
            string connection = @"Data Source=(LocalDB)";
            SqlConnection con = new SqlConnection(connection);
            string sql = "SELECT car, model, year FROM store WHERE @column like @search '";
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);

            sdt.SelectCommand.Parameters.AddWithValue("@column", "%" + searchCars.GetCombo());
            sdt.SelectCommand.Parameters.AddWithValue("@search", "%" + searchCars.GetSearch());

            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = data;
     }

What could possible be the answer to get it search within specific column?

3
  • can you post end - end code from connection opening to connection closing? or explain what is "search", "sdt"? Commented May 2, 2016 at 0:06
  • It doesn't work Alexander. Commented May 2, 2016 at 0:20
  • You can't do that: stackoverflow.com/questions/3330343/… You can use a 'hybrid' method of your two samples. Commented May 2, 2016 at 0:55

2 Answers 2

1

Change it as follows so as to not parameterize the column name:

public int SearchCar(MainStore searchCars)
    {
            string connection = @"Data Source=(LocalDB)";
            SqlConnection con = new SqlConnection(connection);
            string sql = string.Format("SELECT car, model, year FROM store WHERE {0} like @search", search.GetCombo());
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);

           // sdt.SelectCommand.Parameters.AddWithValue("@column", "%" + search.GetCombo());
            sdt.SelectCommand.Parameters.AddWithValue("@search", "%" + search.GetSearch());

            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = data;
     }

Also, you've got an extra quote at the end of your query: like @search '";

Sign up to request clarification or add additional context in comments.

2 Comments

I discovered a new issue. When the combobox is not selected, then a catch error appears. Incorrect syntax near the keyword 'like'
You're going to have to handle the case where it is not selected. I'm guessing search.GetCombo() returns null if it isn't selected. For example, you could try throwing an alert in case nothing is selected. var columnName = search.GetCombo(); if (columnName == null) { \\Show validation message return; }
0

Please take a look at this

 private static void Select() {

       string cmdStr = "SELECT FirstName, LastName, Telephone FROM Person WHERE FirstName = @FirstName";

        using (SqlConnection connection = new SqlConnection(ConnectionString))

        using (SqlCommand command = new SqlCommand(cmdStr, connection)) {

            command.Parameters.AddWithValue("@FirstName", "John");

            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read()) {

                string output = "First Name: {0} \t Last Name: {1} \t Phone: {2}";

                Console.WriteLine(output, reader["FirstName"], reader["LastName"], reader["Telephone"]);

            }

        }

    }

2 Comments

That's not correct at all. I'm using SqlDataAdapter and combobox.
Sorry i did not understand you question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.