C# adding a value and text to a listbox from the codebehind file

OK, I’m adding some functionality to a C# .NET web application and am trying to minimize code impact, both in the app and the database.

This put me in a position where I load a listbox with several values from the Db and for various reasons I move them to other boxes, etc. to do this I’ve got code blocks like this:


obj_ListToWriteTo.Items.Add(lb_Sessions.Items[x]);
lb_Sessions.Items.Remove(lb_Sessions.Items[x]);

Where I’ll add a value to one and remove it from the other. I’ve been using the first two characters of the text value (which is an alphabetic code) to identify and move the rows around.

Now I’ve been informed that sometimes they will add values without this alpha code and I’ll have to use a field from the database that’s not currently being returned, and is not displayed.

The problem with this is I can’t address the value field of the listboxes with the items.add method. I can get the new ID from the Db, but how do I (can I) add data to the “Value” of a row and different data to the display text from the C# codebehind?

I’ve tried this:


lb_Equiv_2.DataTextField = "text: " + x;
lb_Equiv_2.DataValueField = "value: " + x;

and while it doesn’t error, it doesn’t add any values either. And I can’t do a recordset and then databind it, I have to take what they give me and loop through it a couple times because of the data it’s handling, and the way it was collected.

What am I missing?

There is an overload of Add that takes two strings . . .

That’s what I tried first, and I tried it again, just to double-check and got:

There were build errors. Continue?

and in my error tasks window I get:

! No overload for method ‘Add’ takes ‘2’ arguments

NOTE: I’m stuck working in 2003 .NET framework 1.1

Anybody else?

Sorry, wrong API. You need to create a ListItem to add, that can take 2 values. Now, there is a constructor to the ListItem that does take 2 strings. Which is probably what I was thinking of . . .