Solved

SelectedIndexChanged Clear previous value

Posted on 2014-09-19
3
123 Views
Last Modified: 2014-09-23
I have a asp:ListBox with a SelectedIndexChanged that fires. When I click it once it shows a value; however when I click another item that value shows as well as the first value. How can I clear the first value when I click on the listbox again?
0
Comment
Question by:RecipeDan
  • 2
3 Comments
 
LVL 32

Expert Comment

by:it_saige
ID: 40333647
Please provide the code for your SelectedIndexChanged method.

-saige-
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40333853
        protected void PersonList_SelectedIndexChanged(object sender, EventArgs e)
            {
                ListBox list = (ListBox)sender;
                SqlConnection conn;
                SqlCommand comm;
                SqlDataReader reader;
                string connectionString = ConfigurationManager.ConnectionStrings["DConn"].ConnectionString;
                conn = new SqlConnection(connectionString);
                comm = new SqlCommand("SELECT IntNum, FullName, SSN FROM TestNames WHERE IntNum = @ListID", conn);
                comm.Parameters.Add("@ListID", System.Data.SqlDbType.Int);
                comm.Parameters["@ListID"].Value = list.SelectedValue;
                try
                {
                    conn.Open();
                    reader = comm.ExecuteReader();
                    while (reader.Read()) 
                    { 
                        label1.Text += reader["FullName"] + "<br />";
                        label2.Text += reader["SSN"] + "<br />"; 
                    } 
                    reader.Close();
                }
                finally
                {
                    conn.Close();
                }    
            }

Open in new window

0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40333865
The assignment to your labels uses +=.  This is the same as using:

label1.Text = label1.Text + reader["FullName"] + "<br />";

Open in new window


If this was your intent or not the correct issue please let me know.

Otherwise, the below code has been modified to assign the new value to the labels.

protected void PersonList_SelectedIndexChanged(object sender, EventArgs e)
{
	ListBox list = (ListBox)sender;
	SqlConnection conn;
	SqlCommand comm;
	SqlDataReader reader;
	string connectionString = ConfigurationManager.ConnectionStrings["DConn"].ConnectionString;
	conn = new SqlConnection(connectionString);
	comm = new SqlCommand("SELECT IntNum, FullName, SSN FROM TestNames WHERE IntNum = @ListID", conn);
	comm.Parameters.Add("@ListID", System.Data.SqlDbType.Int);
	comm.Parameters["@ListID"].Value = list.SelectedValue;
	try
	{
		conn.Open();
		reader = comm.ExecuteReader();
		while (reader.Read())
		{
			label1.Text = reader["FullName"] + "<br />";
			label2.Text = reader["SSN"] + "<br />";
		}
			reader.Close();
		}
		finally
		{
			conn.Close();
		}
	}
}

Open in new window


HTH,

-saige-
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

12 Experts available now in Live!

Get 1:1 Help Now