Solved

List Box double populating

Posted on 2011-09-29
2
190 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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

6 Experts available now in Live!

Get 1:1 Help Now