Solved

SelectedIndexChanged Clear previous value

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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