Solved

Bug in ListBoxControl ???

Posted on 2004-09-10
14
229 Views
Last Modified: 2010-04-15
I have a webform that uses a listboxcontrol to display a list of users from a table.  Once the form is submitted I would like to have the values passed to the database.  The problem is that only the first int value of the selected users is passing through to the database.  

Can someone evaluate my code and pin point the problem for me.

Listboxcontrol:
<asp:listbox id="Creator" runat="server" SelectionMode="Multiple" Rows="1"></asp:listbox>

CodeBehind:
if(!Page.IsPostBack)
{
SqlConnection Conn;
SqlCommand Cmd;
Conn = new SqlConnection("server=C099450d01;uid=sa;pwd=;database=tools");
Cmd = new SqlCommand("SELECT ID, LastName + ', ' + FirstName AS Name FROM Employee ORDER BY LastName", Conn);
//Creator
Conn.Open();
Creator.DataSource = Cmd.ExecuteReader();
Creator.DataTextField = "Name";
Creator.DataValueField = "ID";
Creator.DataBind();
Conn.Close();

cmd.Parameters.Add(new SqlParameter("@Creator", Creator.SelectedItem.Value));

I can't seem to get all of the values of those selected.

Please Help.

Sincerely.

Tim
0
Comment
Question by:TMF123
  • 6
  • 5
14 Comments
 
LVL 19

Expert Comment

by:drichards
Comment Utility
I assume then that you have a multi-select listbox?  If so, you need to iterate through the selections.  

    for ( int ii = Creator.SelectedIndex; ii < Creator.Items.Count; ii++)
    {
        if ( Creator.Items[ii].Selected )
        {
                // Set the SQL parameter value and insert into database
        }
    }
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
You could also iterate from 0, but the SelectedIndex property gives you the lowest selected index, so nothing less than that will be selected.
0
 

Author Comment

by:TMF123
Comment Utility
Unfortunately, I am still only getting one id for the value.

for ( int ii = Creator.SelectedIndex; ii < Creator.Items.Count; ii++)
  {
       if ( Creator.Items[ii].Selected )
           {
      cmd.Parameters.Add(new SqlParameter("@Creator", Creator.SelectedItem.Value));
           }
   }

I am figuring that I there is a problem with the Creator.SelectedItem.Value part of the above script.

Thank you for your help.
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
What are the parameters for?  Is the command expecting a single parameter and just using the first or last one in the list?  As a test, set a breakpiont inside the for loop and see how many times you hit it.  My guess is that you are getting all the selected values but you are not processing them correctly.
0
 

Author Comment

by:TMF123
Comment Utility
I put a break on the cmd.Paramaters line.

What I am finding is that if I mouse over the following line:
for ( int ii = Creator.SelectedIndex; ii < Creator.Items.Count; ii++)
the Creator.SelectedIndex = 13

If I mouse the this line:
cmd.Parameters.Add(new SqlParameter("@Creator", Creator.SelectedItem.Value));
the Creator.SelectedItem.Value = 15

So something isn't right.

Do you know how I can fix this issue?

Thank you for your comments!!

Sincerely,

Tim
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:TMF123
Comment Utility
drichards,

Oh no.  I've lost you or you've given up on me.  Please come back!

From your last post I have found that the count = 43 which is the number of records in the table.  What I would determine to be happening is that I am retreiveing all of the records from the creator table, the problem is that I am unable to create a second list with just the values of those selected from the listbox control.

I certainly can't believe that what I am attempting to do is foreign to web programming.  I thought that was the sole purpose of this type of control, to introduce one to many relationships between tables etc..

If you get a chance to reply I would greatly appreciate your comments.

Sincerely,

Tim
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
Sorry, I've been out of town for a few days.

I think your problem is here:
-----------------------------
for ( int ii = Creator.SelectedIndex; ii < Creator.Items.Count; ii++)
  {
       if ( Creator.Items[ii].Selected )
       {
           cmd.Parameters.Add(new SqlParameter("@Creator", Creator.SelectedItem.Value));  // <-- you create many parameters.
       }
   }
-------------------------------

What is the command?  I doubt it takes 43 parameters.  You should probably be creating a single parameter before going into the loop and then on each loop iteration you set the parameter and execute the command.  I'll put together some code for you if you need it.

Do this test to confirm:  Check the first and last parameter values added.  You said the first one was value=15 in a previous comment.  Also look at the value the last time in the loop.  I'm betting that the command is executed with one of these two values.
0
 

Author Comment

by:TMF123
Comment Utility
drichards,

Thank you for your patience.

I discovered that I was in fact getting the values.  Once I put a break in teh code and added the control to the watchlist, I could see the selected values in the Creator.SelectedItem.Value.

The problem I am faced with now is how do I get those values to insert into a seperate table so that the values are on a per row basis?

Like this:
ToolUsr_tbl
id             empid           toolid
1                 15               25
2                 17               25
3                 19               25
4                 25               32

That way I can query against the field.

Thank you for your time.

Regards,

Tim
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
I'm not sure aht you're trying yo do here.  Where do 'id', 'empid', and 'toolid' come from?  Is 'id' just an autonumber column in the table?  How are any of these related to the selections in the listbox?  I assume it has something to do with the command you're trying to execute.
0
 

Author Comment

by:TMF123
Comment Utility
I am sorry for the confusion.  Let me start again.

I have a webform that contains a listbox control set for multiple select.  Once the form is complete, then the data is inserted into the Tools database.  However; because the listbox control is allowing for multiple selection, the data submitted from the form for this control must be inserted into a seperate table and a foreign key relationship established.  The reason for this is because if you insert a string of data into a single column within a table, you loose the ability to search against that column.  

In other words, say the user has selected the following names in the control:
Kirk,Spock,Kerry,Bush.
If you query against this column select * where @name = spock you will get nothing in return.  

Therefore I am in search of a method in which I can get each of the values entered into a seperate table at the time the form is submitted with a foreign key relationship to the id of the primary table Tools.

Does this make sense?  I guess I'm just looking for someone to tell me how they handle this scenario.  How do you script a listbox control with multiple select and save the data in a table that is in searchable format?

Thank you so much for not giving up on me.  Although I am begining to wonder if I'm going out of my mind in what I am trying to accomplish here. ;)

Sincerely,

Tim
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
Comment Utility
You have two options here:

1) In the loop that reads all the selections, generate a call to the database on each iteration to insert the current value.

2) Create a dataset and create rows based on the values - one row for each iteration of the selection loop.  Then do one database update at the end using the dataset.

The first option is simpler to code, so you might want to start there.You would make an insert command and either format the text each time through the loop or create a command parameter and set the parameter value each time through the loop.  Either way, you want to also execute the command each iteration of the loop.  And don't create new parameters each time - just set the value of the existing one.  Before entering the loop, create the parameter, and then just set its value on each iteration.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Aspnet xml 2 36
Please explain "Promise Pattern" and how it's used 2 39
Where to begin studying? 5 63
Round up to 100% in .NET 10 41
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now