Solved

List Box double populating

Posted on 2011-09-29
2
193 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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