Cascading Selectboxes from Databasehttp://forums.asp.net/t/1772127.aspx/1?Cascading+Selectboxes+from+DatabaseWed, 22 Feb 2012 10:42:14 -050017721274843350http://forums.asp.net/p/1772127/4843350.aspx/1?Cascading+Selectboxes+from+DatabaseCascading Selectboxes from Database <p>I'm stuck at the following problem - probably an easy one. I got two select boxes (department, persons). When choosing department 1, I wan't only the persons in department one to be displayed. The sql-statements are working with static values (like in the example: 'DEPARTMENT 2'). I just don't get the variable from the select box department in the sql statement dynamically. A hint would be appreciated. Thanks a lot. Here some code snippets:</p> <pre class="prettyprint">var department = Request[&quot;department&quot;]; var selectQueryStringPerson = &quot;SELECT PERSON.SURNAME, PERSON.NAME, DEPARTMENT.DEPARTMENT FROM DEPARTMENT INNER JOIN PERSON ON PERSON.DEPARTMENT_ID = DEPARTMENT.DEPARTMENT_ID where DEPARTMENT.DEPARTMENT = 'DEPARTMENT 2'&quot;; ... @foreach(var row in db.Query(selectQueryStringDEPARTMENT)){ &lt;option&gt;@row.DEPARTMENT&lt;/option&gt; } ... @foreach(var row in db.Query(selectQueryStringFotograf)){ &lt;option&gt;@row.NACHNAME, @row.VORNAME&lt;/option&gt; }</pre> <p>&nbsp;</p> 2012-02-21T14:43:57-05:004843372http://forums.asp.net/p/1772127/4843372.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>If I have understood your problem, the query for populating the second dropdown list is the following:</p> <pre class="prettyprint">var department = Request[&quot;department&quot;]; var selectQueryStringPerson = &quot;SELECT PERSON.SURNAME, PERSON.NAME, DEPARTMENT.DEPARTMENT FROM DEPARTMENT &quot; &#43; &quot;INNER JOIN PERSON ON PERSON.DEPARTMENT_ID = DEPARTMENT.DEPARTMENT_ID where DEPARTMENT.DEPARTMENT = @0&quot;; ... @foreach(var row in db.Query(selectQueryStringDEPARTMENT)){ &lt;option&gt;@row.DEPARTMENT&lt;/option&gt; } ... @foreach(var row in db.Query(selectQueryStringPerson, department)){ &lt;option&gt;@row.NACHNAME, @row.VORNAME&lt;/option&gt; }</pre> <p>&nbsp;</p> 2012-02-21T14:57:42-05:004843417http://forums.asp.net/p/1772127/4843417.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>that's what I did in the first place, but it throws an error: &quot;<span>System.ArgumentNullException: Parameterized query expects a parameter value which was not supplied&quot; Parameter name 0. The values of department are string values, is that a problem?</span></p> 2012-02-21T15:20:12-05:004843447http://forums.asp.net/p/1772127/4843447.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>I think that the error occurs because you execute the query before you have a value for Request[&quot;department&quot;].</p> <p>The simplest solution is to have two pages, one with the first dropdown list and the other with the second dropdown list.</p> <p>When you choose the department in the first list, the form submission redirect to the other page where the second list is populated with the members of the choosen department.</p> 2012-02-21T15:40:09-05:004843731http://forums.asp.net/p/1772127/4843731.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>Can you think of another option within the one form. I have to collect all Information (department, person, other values from user input) and send it all with the post. Is there a possibility to fill the variable of the second list with the selected item of the first one?</p> 2012-02-21T19:11:11-05:004843959http://forums.asp.net/p/1772127/4843959.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>Adjust the following snippet to your goals:</p> <pre class="prettyprint">@{ // ... here your former code var dep = db.Query(selectQueryStringDEPARTMENT); var curDep = dep.First().DEPARTMENT; var selectQueryStringPerson = &quot;SELECT PERSON.SURNAME, PERSON.NAME, DEPARTMENT.DEPARTMENT FROM DEPARTMENT &quot; &#43; &quot;INNER JOIN PERSON ON PERSON.DEPARTMENT_ID = DEPARTMENT.DEPARTMENT_ID where DEPARTMENT.DEPARTMENT = @0&quot;; if(IsPost) { if(!Request[&quot;btn2&quot;].IsEmpty()){ // ... here form processing } curDep = Request[&quot;drop1&quot;]; } var persons = db.Query(selectQueryStringPerson, curDep); } &lt;html&gt; &lt;body&gt; &lt;form action=&quot;&quot; method=&quot;post&quot;&gt; &lt;select name=&quot;drop1&quot;&gt; @foreach(var row in dep){ &lt;option @if(@row.DEPARTMENT == curDep) {&lt;text&gt;selected=&quot;selected&quot;&lt;/text&gt;}&gt;@row.DEPARTMENT&lt;/option&gt; } &lt;/select&gt; &lt;input type=&quot;submit&quot; name=&quot;btn1&quot; value=&quot;Go&quot;/&gt; &lt;select name=&quot;drop2&quot;&gt; @foreach(var row in persons){ &lt;option&gt;@row.NACHNAME, @row.VORNAME&lt;/option&gt; } &lt;/select&gt; &lt;input type=&quot;submit&quot; name=&quot;btn2&quot; value=&quot;Submit&quot;/&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p>When starts, the page takes the first department as default (var curDep = dep.First().DEPARTMENT;) and populates the second dropdown list with persons from this department.</p> <p>If you want to choose another department, you must select it and click the &quot;Go&quot; button.</p> <p>Clicking the &quot;Submit&quot; button starts the form processing.</p> <p>Hope I haven't made mistakes.</p> 2012-02-22T00:10:11-05:004844521http://forums.asp.net/p/1772127/4844521.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>thank you for the example. I wonder if there's another option without the second submit-button. I'd like to dispense with the second button. Is there something like onchange?</p> 2012-02-22T07:23:50-05:004844782http://forums.asp.net/p/1772127/4844782.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>Make these changes:</p> <pre class="prettyprint">..... &lt;body&gt; &lt;form action=&quot;&quot; method=&quot;post&quot; id=&quot;form1&quot;&gt; &lt;select name=&quot;drop1&quot; onchange='document.forms[&quot;form1&quot;].submit();'&gt; .....</pre> <p>Pay attention that it doesn't work if Javascript is disabled in the browser.</p> 2012-02-22T09:12:02-05:004844839http://forums.asp.net/p/1772127/4844839.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>Great, it works! - of course you would say&nbsp;<img src="http://forums.asp.net/scripts/tiny_mce/plugins/emotions/img/smiley-wink.gif" alt="Wink" title="Wink" border="0"></p> <p>Thank you very much.</p> 2012-02-22T09:35:20-05:004844996http://forums.asp.net/p/1772127/4844996.aspx/1?Re+Cascading+Selectboxes+from+DatabaseRe: Cascading Selectboxes from Database <p>I still got a problem with the onchange submit. That's because there is another input-field which allows the user to select several pictures to upload. Now, by changing the department, the selected images gets uploaded - and that's what I want to prevent. So I need a possibility to get the variable from department without submitting the form. Any ideas?</p> 2012-02-22T10:42:14-05:00