Kyle Oliver

Kyle Oliver

  • 2k
  • 67
  • 19.2k

How to clear drop down list?

Apr 2 2019 12:19 PM
Hi,
 
I have 5 drop down lists in my webpage which transfers the data to a database to be stored.
One of the drop down lists is as follows;
 
Male or Female
  1. -- Select --  
  2. Male  
  3. Female 
Once I press the "Add Employee" button the form must clear to be able to add another employee.
I am able to clear the text boxes on the form but the code I am using to clear the drop down list, clears all the list items in the drop down list and I am unable to use the list items in the dropdown list when I need to add another employee.
 
Drop down list code;
  1. DropDownWorkforceProfileAddNewRace.Items.Clear();  
This code clears all the list items in the drop down list, which is not what i need.
 
How can I clear the drop down selected value when the "Add Employee" button is pressed and still keep the list items in the drop down list?
 
My code for the webpage below;
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9.   
  10. namespace EmploymentEquity  
  11. {  
  12.     public partial class WorkforceProfileAddEmployee : System.Web.UI.Page  
  13.     {  
  14.         SqlConnection con = new SqlConnection(@"Data Source=KYLELT\SQLEXPRESS;Initial Catalog=EmploymentEquity;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");  
  15.   
  16.         protected void Page_Load(object sender, EventArgs e)  
  17.         {  
  18.   
  19.         }  
  20.   
  21.         protected void BtnAddNewEmployeeWorkforceProfile_Click(object sender, EventArgs e)  
  22.         {  
  23.             con.Open();  
  24.             SqlCommand cmd = con.CreateCommand();  
  25.             cmd.CommandType = System.Data.CommandType.Text;  
  26.             cmd.CommandText = "insert into WorkforceProfile values('"+TxBxWorkforceProfileAddNewEmployeeCode.Text+ "','" +TxBxWorkforceProfileAddNewEmployeeCode.Text+ "','"+TxBxWorkforceProfileAddNewEmployeeName.Text+ "','"+DropDownWorkforceProfileAddNewMaleFemale.SelectedValue+ "','"+DropDownWorkforceProfileAddNewRace.SelectedValue+ "','"+TxBxWorkforceProfileAddNewJobTitle.Text+ "','"+DropDownWorkforceProfileAddNewOccLevel.SelectedValue+ "','"+TxBxWorkforceProfileAddNewAnnualSalary.Text+ "','"+DropDownWorkforceProfileAddNewForiegnNational.SelectedValue+ "','"+DropDownWorkforceProfileAddNewDisabled.SelectedValue+"')";  
  27.             cmd.ExecuteNonQuery();  
  28.             con.Close();  
  29.   
  30.             LblAddEmployeeSuccess.Text = "Employee Added Successfully";  
  31.   
  32.             TxBxWorkforceProfileAddNewEmployeeCode.Text = String.Empty;  
  33.             TxBxWorkforceProfileAddNewEmployeeName.Text = String.Empty;  
  34.             DropDownWorkforceProfileAddNewMaleFemale.Items.Clear();  
  35.             DropDownWorkforceProfileAddNewRace.Items.Clear();  
  36.             TxBxWorkforceProfileAddNewJobTitle.Text = String.Empty;  
  37.             DropDownWorkforceProfileAddNewOccLevel.Items.Clear();  
  38.             TxBxWorkforceProfileAddNewAnnualSalary.Text = String.Empty;  
  39.             DropDownWorkforceProfileAddNewForiegnNational.Items.Clear();  
  40.             DropDownWorkforceProfileAddNewDisabled.Items.Clear();  
  41.   
  42.             Response.Redirect("WorkforceProfileAddEmployee.aspx");  
  43.   
  44.         }  
  45.   
  46.         protected void LinkButton1_Click(object sender, EventArgs e)  
  47.         {  
  48.             Response.Redirect("WorkforceProfile.aspx");  
  49.         }  
  50.     }  


Answers (3)