Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist has a SelectedValue which is invalid because it does not

Status
Not open for further replies.

Katya85S

Programmer
Jul 19, 2004
190
Perhaps somebody can help… In asp.net project (VB.net) I want to populate a dropdownlist, lst1, with a value from SQL Query, using SLQDataReader object.
strSQL=”SELECT AppName, EmpNum FROM tblApp WHERE AppID=” & iAppID
myConn.Open()
Dim objCmd As New SqlCommand(strSQL, myConn)
Dim objDR As SqlDataReader
objDR = objCmd.ExecuteReader(CommandBehavior.CloseConnection)

If objDR.Read Then
lstEmpIT.SelectedValue = objDR("EmpNum")
end if
This code results in an error message:
“'lst1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value”
I’ve trouble-shoot the objDR(“EmpNum”) to find out its value:
lblMessage.Text = objDR("EmpNum")
This returned value “00345”.
When I hard-coded the value into the code:
lstEmpIT.SelectedValue = “00345”
the application workes fine, populating the correct item into the dropdownlist lst1.
What could be wrong with objDR(“EmpNum”) and how can I fix the situation?
I would appreciate any advice.
Thank you all in advance.
 
Never mind. I have fixed it:
lst1.SelectedValue = CStr(Right((100000 + objDR("EmpNum")), 5))
does the thing.
So it looks like it was loosing leading 0's, while assigning objDR("EmpNum") to the lst1.SelectedValue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top