2

I am trying to pass parameter for below select statement in postgresql, but it is not returning any row,

cmd.Parameters.AddWithValue("@name", richTextBox_searchEmp.Text);                
string sql = "select * from tbl_emp_Info where LOWER(firstname) like LOWER('@name%');";

where- richTextBox_searchEmp.Text is “first” have also tried -

cmd.Parameters.AddWithValue("@name", NpgsqlDbType.Char , searchEmp.Text);

while, parameter less query below always returning correct results.

 string sql = "select * from tbl_emp_Info where LOWER(firstname) like LOWER('first%');";

Please help!!!

Complete Code-

            conn.Open();
            cmd.Parameters.AddWithValue("@name", NpgsqlDbType.Char , richTextBox_searchEmp.Text);

            string sql = "select * from tbl_emp_Info where LOWER(firstname) like LOWER('@name%');";

            NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);

            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();

            ds.Reset();
            da.Fill(ds);
            dt = ds.Tables[0];
            dataGridView.DataSource = dt;

1 Answer 1

7

Pass your parameter with % like

Change you query to

string sql = "select * from tbl_emp_Info where LOWER(firstname) like LOWER(@name);";

And Pass @name like

cmd.Parameters.AddWithValue("@name", "%" + searchEmp.Text + "%"); 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Dnyanesh, tried this but not worked :( I have implemented it in following way, but still searching for Parameterized way to do it. string sql = "select * from tbl_emp_Info where LOWER(firstname) like LOWER('" + richTextBox_searchEmp.Text + "%');";
My bad remove single quote from sql string. so it will be @name and not '@name'. Now this should work. I have tested and it is working well. % before is optional if you want to search text anywhere in string.

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.