dhivya krishnamoorthy dhivya krishnamoorthy - 4 months ago 13
C# Question

Checkbox list need to be checked based on the values saved in DB

chkTypeofIncident.DataSource = from t2 in
dbl.usp_GetIncidentsType().AsQueryable()
where t2.IncidentTypeFlag== '2' select t2;

chkTypeofIncident.DataTextField = "Incidents";
chkTypeofIncident.DataValueField = "IncidentsMasterID";
chkTypeofIncident.DataBind();


I got the selected values using LINQ. but output was it always checked the last value in database.

var CheckedInc = from t2 in dbl.usp_GetIncidentsTypeDetail(Convert.ToInt32(HiddenIncidentRegisterID.Value)).AsQueryable()
select t2;
foreach(var chk in CheckedInc)
{
chkTypeofIncident.SelectedValue = chk.IncidentsMasterID.ToString();
}

Answer
foreach (var chk in CheckedInc)
{
chkTypeofIncident.Items.FindByValue(chk.IncidentsMasterID).Selected = true;
}