Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
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 29-Jan-13 2:24am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First prepare string variable which contains all selected values in listbox separated by comma(,) with following code
 
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 Smile | :)
  Permalink  
Comments
Santhosh23 at 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
kshitij_CodeMaster at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
select client_id in (1,2,3) -- where 1,2,3 are selected client ids.
  Permalink  
v2
Comments
Santhosh23 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 450
1 OriginalGriff 230
2 DamithSL 150
3 Dave Kreskowiak 110
4 Suvendu Shekhar Giri 110
0 OriginalGriff 7,740
1 DamithSL 5,644
2 Sergey Alexandrovich Kryukov 5,404
3 Maciej Los 5,011
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100