Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SelectedIndexChanged Clear previous value

Posted on 2014-09-19
3
Medium Priority
?
133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

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 34

Accepted Solution

by:
it_saige earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

688 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