c# - Getting selected items from a listbox using the index instead of value -


when fill listbox, this:

sqldataadapter adapter = new sqldataadapter("select [ddlvalue], [storedvalue]  [tmpdropdowns] ddlname = 'ddlhobbies' order [sortorder] asc", conn);  adapter.fill(ddlhobbies);  hobbies.datasource = ddlhobbies; hobbies.datatextfield = "ddlvalue"; hobbies.datavaluefield = "storedvalue"; hobbies.databind(); 

and when want retrieve items, this:

var selectedhobbies = hobbies.items.cast<listitem>().where(item => item.selected); string strhobbies = string.join(",", selectedhobbies).trimend(); 

unfortunately, gives me this:

strhobbies = "fishing,skiing,pool,birdwatching" 

i "unfortunately" because objective use strhobbies in "where" clause in sql string. so, sql string should like:

select * mytable hobbies in (strhobbies) 

so, there's 2 pieces problem.

  1. how change "var selectedhobbies" line pull in storedvalue rather ddlvalue

  2. how restructure "string strhobbies" line put double quotes around each item, can use in 'in' clause?

i'm thinking want final sql string more like:

select * mytable hobbies in ("2", "5", "6", "9") 

edit:

i've changed 1 thing:

list<string> selectedhobbies = hobbies.items.cast<listitem>()     .where(item => item.selected).select(item => item.value).tolist();  string strhobbies = string.join(",", selectedhobbies).trimend(); 

this gives me storedvalue instead of ddlvalue when hover on selectedhobbies, when type '?strhobbies' in immediate window, says, "the name strhobbies not exist in current context". so, i've got #1 down, if me #2 i'd appreciate it.

for #2 want single quotes around each value, not double quotes. (assuming hobbies not integer in db)

string strhobbies = string.join("','", selectedhobbies).trimend(); 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -