'SqlDataAdapter error in c sharp
Hello I have an error with the data adapter in c sharp . How to fix?
SqlCommand cmd = new SqlCommand("select * from View_1 where Words_Sh LIKE ' + @txbSearch + '%'", con);
cmd.Parameters.AddWithValue("@txbSearch", this.txbSearch.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd, con)
;
Solution 1:[1]
Don't add a single quote and the plus sign before the parameter placeholder
SqlCommand cmd = new SqlCommand("select * from View_1 " +
"where Words_Sh LIKE @txbSearch + '%'", con);
Also, I prefer to concatenate the wildcard symbol directly inside the parameter value.
Not sure if it makes any difference, though, just a matter of preferences and less clutter in the query string.
SqlCommand cmd = new SqlCommand("select * from View_1 " +
"where Words_Sh LIKE @txbSearch", con);
cmd.Parameters.AddWithValue("@txbSearch", this.txbSearch.Text + "%");
Solution 2:[2]
EXAMPLE:
string commandText = "select * from View_1 " + "where Words_Sh LIKE @parameters" cmd.Parameters.AddWithValue("@parameters", "Parameter 1");
Solution 3:[3]
Because of starting double quote, you should finish with double quotes before plus sign, but you can use single quote before double quote for LIKE operation.
SqlCommand cmd = new SqlCommand("select * from View_1 where Words_Sh LIKE '@txbSearch%'", con);
cmd.Parameters.AddWithValue("@txbSearch", this.txbSearch.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Önder ÖZDEM?R |
| Solution 3 | burakarat |
