displaying results on dropdownlist
-
Hi I am displaying locations on dropdownlist so the problem is if i have selected record which is not in the database it gives me this error" 'ddlLocation' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value " here is my code and is working perfect if the record is in the database If strPerNo = "" Then Session("samacc") = HttpContext.Current.User.Identity.Name.ToLower emp = cEmp.GetEmp(Session("samacc")) 'empPhoto = cEmp.DisplayPhoto(emp.perno) Else emp = cEmp.GetEmpByPerNo(strPerNo) 'btnAdmin.Text = "UPDATE" End If If emp.perno = "" Then Response.Write(Session("samacc") + " not found") Else Session("perno") = emp.perno Response.Write(emp.name + " " + emp.surname) Session("ImgPerNr") = emp.perno txtFirstname.Text = emp.name txtLastname.Text = emp.surname txtDivision.Text = emp.BsUnit lblBU.Text = emp.BsUnit txtBlock.Text = emp.block txtFloor.Text = emp.floorno txtRoom.Text = emp.roomno txtInt.Text = emp.Int1 txtTel.Text = emp.tel txtAltTelNo.Text = emp.alttel txtCell.Text = emp.cellno txtAltCell.Text = emp.altcellno txtEmail.Text = emp.email txtFax.Text = emp.fax txtAltEmail.Text = emp.altemail If ddlLocation.SelectedValue <> "0" Then ddlLocation.SelectedValue = emp.LocID Else ddlLocation.SelectedValue = 0 End If
Mamphekgo
-
Hi I am displaying locations on dropdownlist so the problem is if i have selected record which is not in the database it gives me this error" 'ddlLocation' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value " here is my code and is working perfect if the record is in the database If strPerNo = "" Then Session("samacc") = HttpContext.Current.User.Identity.Name.ToLower emp = cEmp.GetEmp(Session("samacc")) 'empPhoto = cEmp.DisplayPhoto(emp.perno) Else emp = cEmp.GetEmpByPerNo(strPerNo) 'btnAdmin.Text = "UPDATE" End If If emp.perno = "" Then Response.Write(Session("samacc") + " not found") Else Session("perno") = emp.perno Response.Write(emp.name + " " + emp.surname) Session("ImgPerNr") = emp.perno txtFirstname.Text = emp.name txtLastname.Text = emp.surname txtDivision.Text = emp.BsUnit lblBU.Text = emp.BsUnit txtBlock.Text = emp.block txtFloor.Text = emp.floorno txtRoom.Text = emp.roomno txtInt.Text = emp.Int1 txtTel.Text = emp.tel txtAltTelNo.Text = emp.alttel txtCell.Text = emp.cellno txtAltCell.Text = emp.altcellno txtEmail.Text = emp.email txtFax.Text = emp.fax txtAltEmail.Text = emp.altemail If ddlLocation.SelectedValue <> "0" Then ddlLocation.SelectedValue = emp.LocID Else ddlLocation.SelectedValue = 0 End If
Mamphekgo
mamphekgojakes wrote:
ddlLocation.SelectedValue = emp.LocID
Does the value of emp.LocID exist in the Drop down. You get such an error when you set the selectedvallue to a value that doesnt exist in the Drop down item collection
-
mamphekgojakes wrote:
ddlLocation.SelectedValue = emp.LocID
Does the value of emp.LocID exist in the Drop down. You get such an error when you set the selectedvallue to a value that doesnt exist in the Drop down item collection
if the value does exist how can assign 0 to non existing values
Mamphekgo
-
if the value does exist how can assign 0 to non existing values
Mamphekgo
you add like a "Select One" Item to the drop down list with a value of 0, and then you set the selected value to 0 if the item dont exist. So you have to loop the dropdown to check if the value you are setting exist or not.