Click here to Skip to main content
15,886,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two departments in dropdowlist
Hardware
Software

the Dropdown is binding departments from database and its binding fine.
My problem is while editing the dropdown is not fetching the current value.


if (!IsPostBack)
{
DataTable Dt = objdatahandler.GetallDepartments();

ddlPblemtype.DataSource = Dt;
ddlPblemtype.DataTextField = "DepartmentName";
ddlPblemtype.DataValueField = "DepartmentID";
ddlPblemtype.DataBind();


int regaid = Convert.ToInt32(Request.QueryString["id"]);
objcomreg.regid = regaid;
DataTable Ds = objdatahandler.getcomplaintbyid(objcomreg);
if (Ds.Rows.Count > 0)
{

ddlPblemtype.SelectedValue = Ds.Rows[0]["Department"].ToString();
}

}
Posted
Comments
Sinisa Hajnal 25-Nov-14 4:26am    
Is your Ds.Rows[0]["Department"] value DepartmentID? in the binding you set value as DepartmentID...
ManojMurali 25-Nov-14 5:31am    
DataTable Dt = objdatahandler.GetallDepartments();

ddlPblemtype.DataSource = Dt;
ddlPblemtype.DataTextField = "DepartmentName";
ddlPblemtype.DataValueField = "DepartmentID";
ddlPblemtype.DataBind();

now the dropdown list is binded and have values in it

now while editing the row from grid view its selecting the values

int regaid = Convert.ToInt32(Request.QueryString["id"]);
objcomreg.regid = regaid;
DataTable Ds = objdatahandler.getcomplaintbyid(objcomreg); // values binded


ddlPblemtype.SelectedValue = Ds.Rows[0]["Department"].ToString(); // not geting the value , showing the first value from the list.


Sinisa Hajnal 25-Nov-14 5:54am    
If you want to select VALUE you have to provide DepartmentID. If it is int value try casting into INT instead of ToString, but the important part is that Ds.Rows[0]["Department"] MUST CONTAIN DepartmentID not DepartmentName
Abhilas_Mohanty 25-Nov-14 4:47am    
Can we look at the Query string and the data table values....

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