2014年7月13日 星期日

[RESOLVED] Count error in query


in my asp.net+vb+access database web. in a page ia m using a query to count.. the following code works fine


qstr8 = "select count(*) from pers where OutDetails like '%" + DropDownList1.Text + "%'"


sample data in outdetails field


outdetails    


121 tata spares section


21 tata spares section


12 tata spares section


 problem is when the query is run and the dropdown selected is 121 tata spares section


it also counts 121 tata spares section ans 21 tata spares section


thus the count gets wrong



Try this,


qstr8 = "select count(*) from pers where OutDetails like '" + DropDownList1.Text + "%'"

OR if you are trying to find the string in the middle of the long string then you can add an extra space.


qstr8 = "select count(*) from pers where OutDetails like '% " + DropDownList1.Text + "%'"


You are doing a wild data match with '% " + DropDownList1.Text + "%'", thats' your probelm.


If you don't need to match both sides of the string, you can just match the string that macthes the start of the string


select count(*) from pers where OutDetails like DropDownList1.Text + "%'"



沒有留言:

張貼留言