Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void rightsload()
{
DataSet EmpDs = new DataSet();
EmpDs = (DataSet)Session["EmpId"];
DataRow dr = EmpDs.Tables[0].NewRow();
dr["EmpName"] = "All";
dr["EmpId"] = 0;
EmpDs.Tables[0].Rows.InsertAt(dr, 0);

if (EmpDs.Tables[0].Rows.Count > 0)
{

DataView view = new DataView(EmpDs.Tables[0], null, "EmpId", DataViewRowState.CurrentRows);
ddlEmp.DataTextField = "EmpName";
ddlEmp.DataValueField = "EmpId";
ddlEmp.DataSource = view;
ddlEmp.DataBind();
}
}




The "All" multiplies during each postback and thus leads to postback of page too
Posted

1 solution

EmpDs = (DataSet)Session["EmpId"];

is showing you have Dataset in session.

when you are saving data in Session["EmpId"] at that time you are running SQL Query in data base.
use DISTINCT Key word in your SQL Query.


Like
SELECT DISTINCT EmpId FROM Your_EMP_TABLE



FOR 2nd Problem

write this code block in IsPostBack check Like

if(!IsPostBack){
DataRow dr = EmpDs.Tables[0].NewRow();
dr["EmpName"] = "All";
dr["EmpId"] = 0;
EmpDs.Tables[0].Rows.InsertAt(dr, 0);

}

I would suggest call rightsload() in IsPostBack check
becouse you need run this method only once when page loads 1st time, not at every postback.

Like

if(!IsPostBack)
{
rightsload();
}
 
Share this answer
 
v2

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