Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help! set selected items in listbox selected from database items

Status
Not open for further replies.

dzr

Programmer
Nov 20, 2001
109
0
0
US
I have an array of numbers 3,6,10,15 and they print just fine in a loop but when I try to set the multiselect items they match, I get a "Object reference instance of an object." error.


html output:

<select size="4" name="jobtypes" multiple="multiple" id="jobtypes">
<option value="1">Teacher</option>
<option value="2">Nurse</option>
<option value="3">Fireman</option>
<option value="4">Waitress</option>
...
</select>

codebehind:

foreach (string s in jobtitles)
{
jobtitles.Items.FindByValue(s).Selected = true;

}

Any input would be greatly appreciated!!!
 
Just out of curiosity... is your codebehind supposed to reference "jobtypes" or "jobtitles"?
 
job types is the array of job titles a user has selected - comma delimited, coming from the DB and split up into just number values. Field is nvarchar in the DB.

all the examples on the web are from post-back forms so i couldn't figure out if i needed to loop through the multi-select items and have a loop inside that loop comparing it with the db values??

anyone have an example?

thanks!
 
there were some typos in my above post... this is more clear:

everything below works as expected until #6 where I try to have the multi-select listbox display the selected items in an edit form.

1. form code --i'm populating the listbox with items from the 'jobs' table using a select statment

<td align="right" valign="top">*Community Jobs Available</td>
<td valign="middle" style="width: 323px"><asp:ListBox ID="choosejobs" runat="server" DataSourceID="SqlDataSource1"
DataTextField="job_title" DataValueField="job_id" SelectionMode="Multiple" ></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT * FROM [jobs] WHERE ([job_location] = @job_location)">
<SelectParameters>
<asp:parameter DefaultValue="San Francisco" Name="job_location" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</td>

2. form code rendered in browser:
<select size="4" name="choosejobs" multiple="multiple" id="choosejobs">
<option value="1">Teacher</option>
<option value="2">Nurse</option>
<option value="3">Fireman</option>
<option value="4">Waitress</option>
<option value="4">Doctor</option>
...
</select>


3. when submitted - i turn it into a comma delimted set of job_id's like this--> 2,5,6 and write to a db record for the user

4. exact same set up as #1 for up edit form
[the edit form is in a seperate location (only available to Admin and has some extra fields)]

5. select * from user_jobs -- get user's list of jobs
string userjobs = dbreader["userjobs"] --> which makes userjobs = "1,2,5"
turn into array of string values (this works fine - I have printed it to the screen and all values are coming out ok)

6. loop through that array to set selected
foreach (string j in userjobs)
{
choosejobs.Items.FindByValue[j].Selected = true;
}

7. error! "System.NullReferenceException: Object reference not set to an instance of an object."


i have tried this: choosejobs.Items.FindByValue("3").Selected = true;
and get the same error so I think I'm something wrong in calling the Items.FindByValue ??

i would appreciate any help please!
 
Looks like there comes a point when choosejobs.Items.FindByValue[j] does not reference an item i.e. no item has the value j. You could do...

Code:
if (choosejobs.Items.FindByValue[j] != null)
{
  choosejobs.Items.FindByValue[j].Selected = true;
}

...or use try/catch....

Tom
emo_big_smile.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top