Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have one from with list box, my listbox shows client names and selection mode is multiple. am select the 3 or more items then click on search button.

single selection is working fine and display data correct. but 1 or more selection in my list box query is not working. how to write the query.?

in database client-name field is there.

how to search the data single column with multiple client names.i.e am given to client_name='san,jhone,dev' then display the related data on these client-names.

how to display please help me
Posted

First prepare string variable which contains all selected values in listbox separated by comma(,) with following code

C#
string selecteditems = "";
for (int i = 0; i < listBox1.Items.Count; i++)
{
 if (listBox1.GetSelected(i))
 {
   selecteditems += "'"+listBox1.GetItemText(listBox1.Items[i])+"',";
 }
}
selecteditems = selecteditems.Remove(selecteditems.Length - 1);



Now write ur select query as

" Select * from clienttable where client_name in ("+ selecteditems +")"
this will surely help u..

hav a great time.
If it helps u dnt forget to click on accept ans and vote for it.
enjoy :)
 
Share this answer
 
Comments
Santhosh23 29-Jan-13 8:08am    
thank for your replay,

just now am tring your query
am selection two names in listbox.

SELECT RECORDID,AVAILABILITY,EMPLOYEMENTSTATUS,CLIENTNAME,DOB,SKILLS,EMAILID,VISA,CREATEDDATE,LOCATION,CITY+', '+C_STATE as LOCATION
FROM dbo.RECORDDETAILS where CLIENTNAME='newclient,gdgf'

but no records found my query like this
[no name] 29-Jan-13 8:11am    
rewrite query as
SELECT RECORDID,AVAILABILITY,EMPLOYEMENTSTATUS,CLIENTNAME,DOB,SKILLS,EMAILID,VISA,CREATEDDATE,LOCATION,CITY+', '+C_STATE as LOCATION
FROM dbo.RECORDDETAILS where CLIENTNAME in ('newclient','gdgf')
Santhosh23 29-Jan-13 10:19am    
your query working sql statement. same query how to write in stored procedure
am execute procedure

SELECT RECORDID,AVAILABILITY,EMPLOYEMENTSTATUS,CLIENTNAME,DOB,SKILLS,EMAILID,VISA,CREATEDDATE,LOCATION,CITY+', '+C_STATE as LOCATION FROM dbo.RECORDDETAILS where CLIENTNAME in (@CLIENTNAME)

but exec SP_GET_RECORDDETAILS1 'Client Test','IBM','newclient' shows the error too many parameters.
exec SP_GET_RECORDDETAILS1 'Client Test,IBM,newclient'
its show no records
how to write same query in stored procedure.
Alternatively, bind your listbox with setting DataValueField property as client_id and DataTextField as client_name.

You can send the Client_ID's [assuming that column is present in your database] as comma separated values and in select query you can write
SQL
select client_id in (1,2,3) -- where 1,2,3 are selected client ids.
 
Share this answer
 
v2
Comments
Santhosh23 29-Jan-13 7:39am    
thank for the your replay.
actually i don't have client_id field. and am passing the client-names. depend on user selection. so how to split the values by using , in database. actually we don't no for how many selections in client names

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900