Link to home
Start Free TrialLog in
Avatar of TMF123
TMF123

asked on

Almighty Expert Needed!!!!!!!

I am at my wits end with attempting to retrieve the values selected for a dropdown list control.  I am having no problems retrieving the text results of those values selected however; I cannot seem to get all of the values selected to show up.  Only the first value of the first item selected in the dropdownlist is retrieved.

Can someone please help me discover the woes of my code?

Here is the script for the control:
<asp:listbox id="Creator" runat="server" SelectionMode="Multiple" Rows="1"></asp:listbox>

Here is the script to gather the data selected from the control:
SqlConnection conn = new SqlConnection("server=C099450d01;uid=sa;pwd=;database=tools");
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("Select UID, LastName +','+ FirstName From Employee Where UID = @UID ORDER BY LastName", conn);
conn.Open();
foreach (ListItem myItem in Creator.Items)
{
if (myItem.Selected == true)
{
if (lblCreator.Text.Length > 0)
{
cmd.Parameters.Add("@UID", SqlDbType.Real).Value = Creator.SelectedValue;
lblCreator.Text += " - ";
}
lblCreator.Text += myItem.Text;

What is happening is that if I put a break point in the build located at the end of this script I can see that the "Creator.Items" shows me all of those selected from the ddl.  However if I mouseover the "Creator.SelectedValue" I am only seeing one value where I should be seeing a value for all selected.

Any help would be greatly appreciated.

Sincerely,
Tim
Avatar of chmohan
chmohan

did u try SelectedItem.Text instead of Value?
Avatar of TMF123

ASKER

The problem is that I do not want the text to be submitted to the db but instead the values that the text represent.  In other words, for each text item in the ddl there is an integer value because the ddl is populated by a table of employees.

Farrell, Timothy = 1
Sachs, Joel = 2

and so on...

My problem is I can't seem to extract the values of those items selected in the ddl.  Only the first value of the first item selected shows up.

I need to have the int value in the db so I can perform searches and queries against the column.

Tim
okay got u,use  a   datareader:)
Avatar of TMF123

ASKER

Interesting...

Oddly enough it would appear that the code submitted is in vb not c#(where the user posted his question).

 I also need to make reference to c# since that is what I am using.  I will have to locate other resources for understanding how to implement a datareader for this purpose.
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
    // Always call Read before accessing data.
    while (myReader.Read()) {
     Combobox3.Items.Add(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    }
    // always call Close when done reading.
    myReader.Close();


Avatar of TMF123

ASKER

This appears to be collecting the text values from the ddl:

Combobox3.Items.Add(myReader.GetInt32(0) + ", " + myReader.GetString(1));

lastname, firstname

Is there a way to get this to collect the values or the integers for all selected?

Currently I get an error stating:

System.Data.SqlClient.SqlException: Must declare the variable '@UID'.

Thank you for hanging in there.
oh okay got u

see this link and see function public void ReadData()

http://www.thecodeproject.com/useritems/SmartReader.asp

so i think u got to use  

Convert.ToInt32(Reader[“ UID”])    right?
Avatar of TMF123

ASKER

I get an error from the server attempting to access this page.
ASKER CERTIFIED SOLUTION
Avatar of chmohan
chmohan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial