Solved

List Box double populating

Posted on 2011-09-29
2
194 Views
Last Modified: 2012-06-27
I have a new one for you guys...

I have a list box that is populated via a stored procedure .... Now here is the wierd thing my list box is being double populated... that is if I have only two names in my database where the reader is reading from i.e. Adams and Zach it is printing them ouit twice adams zach adams zach...

its like the method is being called twice...

does anyone know how to stop it from doing that???

Matt



SqlConnection con = new SqlConnection(Settings.GetConfigValue("PARK_IRB_V1_11_19_2010", "IRB2010ConnectionString"));
            con.Open();
            SqlCommand cmd = new SqlCommand("P_sp_PopulateNewFactSup", con);
            SqlDataReader r = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            while (r.Read())
            {
                ListItem i = new ListItem(r["truename"].ToString().Trim(), r["ParkID"].ToString().Trim());
                LBFactSupNew.Items.Add(i);
            }
            con.Close();

Open in new window

0
Comment
Question by:HawaiiDragon
2 Comments
 
LVL 11

Accepted Solution

by:
brutaldev earned 500 total points
ID: 36817475
Setup breakpoints and check the stack trace for each call that goes into the method code you have provided. That way you can find why the extra call is getting made.

To avoid the list getting extra items in the case where you can't stop the extra calls, just clear it at the beginning of while loop:
...
LBFactSupNew.Items.Clear();
while (r.Read())
{
  ListItem i = new ListItem(r["truename"].ToString().Trim(), r["ParkID"].ToString().Trim());
  LBFactSupNew.Items.Add(i);
}
...

Open in new window

0
 

Author Closing Comment

by:HawaiiDragon
ID: 36817486
I did this thank you for your help

 SqlConnection con = new SqlConnection(Settings.GetConfigValue("PARK_IRB_V1_11_19_2010", "IRB2010ConnectionString"));
            con.Open();
            SqlCommand cmd = new SqlCommand("P_sp_PopulateNewFactSup", con);
            SqlDataReader r = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            LBFactSupNew.Items.Clear();
            while (r.Read())
            {
                ListItem i = new ListItem(r["truename"].ToString().Trim(), r["ParkID"].ToString().Trim());
                LBFactSupNew.Items.Add(i);
            }
            con.Close();
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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