Solved

SelectedIndexChanged Clear previous value

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

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 33

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
Need help with a Crystal Report in my C#.Net application 2 34
C# Formatting DateTime String Value 5 50
WPF Select Items 3 24
orderby list (from Json) 1 27
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 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