Solved

SelectedIndexChanged Clear previous value

Posted on 2014-09-19
3
130 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

615 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