Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a 3 items in drop downlist such as admin user and supervisor. i want to write a code so that no one will delete my admin item. other items can be deleted. How will i do this
Posted
Comments
Charan_Kumar 10-Mar-14 4:56am    
where you are giving option to delete item?
Member 10578683 10-Mar-14 5:22am    
i have a delete button. I wrote code for delete in the click event of the button. but out of the list of items in my drop downlist i don't want to delete Admin item.how will i do
Aravindba 10-Mar-14 6:34am    
form where u delete ? form db ? How u bind item in dropdown box? in design time or run time ? can u post ur delete query ? r delete form table and rebind dropdown box ?

C#
Check the Dropdownlist SelecetedItem.value (say 1 for admin)
if(1)
{
//do nothing
}
else
{
//delete code here
}
 
Share this answer
 
Hai

Try like this,i think u delete data form table and rebind dropdown,

VB
Delete from Department where Dept <> "Admin"



C#
if (DropDownList1.Text == "Administrator")
       {

       //Leave it as empty

       }
       else
       {
           DropDownList1.Items.Remove(DropDownList1.SelectedItem);
           con.Open();
           string query ="Delete from Role1 Where Role_Name ='" + DropDownList1.Text +"'";
           SqlCommand cmd = new SqlCommand(query, con);
           cmd.ExecuteNonQuery();
           con.Close();
       }


I don't know what u exact needs,in dropdownbox u remove select item only,but in Role1 table u remove all data excluding Administrator.

How to bind data in dropdownbox ? in design time u add items or dynamically bind data form table.If u bind data from table dynamically to dropdownbox then try below code


C#
if (DropDownList1.Text == "Administrator")
       {

       //Leave it as empty

       }
       else
       {
          // DropDownList1.Items.Remove(DropDownList1.SelectedItem);
           con.Open();
           string query ="Delete from Role1 Where Role_Name ='" + DropDownList1.Text +"'";
           SqlCommand cmd = new SqlCommand(query, con);
           cmd.ExecuteNonQuery();
           con.Close();
         Binddropdown();//Here u call the code to bind data to dropdownbox
       }


I think how u know to bind data to dropdownbox dynamically in Bind dropdown function u write code to bind data like dropdownlist1.ds; dropdownlist1.bind ect...
Regards
Aravind
 
Share this answer
 
v4
Comments
Member 10578683 10-Mar-14 7:29am    
protected void Button2_Click(object sender, EventArgs e)
{


DropDownList1.Items.Remove(DropDownList1.SelectedItem);
con.Open();
string query = "Delete from Role1 Where Role_Name <> 'Administrator' ";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();

}
Member 10578683 10-Mar-14 7:30am    
this is my code for delete
Aravindba 10-Mar-14 23:12pm    
see my update solution
Dont let all users to delete the datas. Instead of that, give full rights only to Admin.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900