Answered by:
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items

Question
-
User-1907931422 posted
Hello, I know that this is a notorious issue and there are many disscussion online but they are not fit for my case. Therefore I open this thread, hopefully I can get an advice.
I have a dropdown list
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true"> </asp:DropDownList>
In the code behind, I filled the dataset as below:
cmd.CommandText = "SELECT [FirstName], [LastName], [Degree], [Organization], [Phone],[Ext] FROM [User_ExtraInfo] WHERE [UserID] = @UserID"; string UserID = user.ProviderUserKey.ToString(); cmd.Parameters.Add("@UserID", SqlDbType.VarChar).Value = UserID; DataTable dt = new DataTable(); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); } if (dt.Rows[0]["Organization"].ToString() == null || dt.Rows[0]["Organization"].ToString() == "") DropDownList1.SelectedValue = "Please Select"; else DropDownList1.SelectedValue = dt.Rows[0]["Organization"].ToString();
When I stepped through the code, I found the value of dt.Rows[0]["Organization"].ToString() is not null or empty but DropDownList1.SelectedValue is null.
Finally I got an error:
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items
Thanks.
Wednesday, December 28, 2011 9:00 AM
Answers
-
User2053451246 posted
else
DropDownList1.Items.Add(dt.Rows[0]["Organization"].ToString()) <-- add this line
DropDownList1.SelectedValue = dt.Rows[0]["Organization"].ToString();- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 28, 2011 9:25 AM -
User2053451246 posted
Put inside a loop:
DataRow dr = null; foreach (DataRow dr in dt.Rows()) { DropDownList1.Items.Add(dr["Organization"].ToString()); }
I'm a VB programmer. Hopefully my code converted to C# correctly for you. :)- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 28, 2011 9:53 AM
All replies
-
User2053451246 posted
You are trying to set it to "Please Select" but you don't have that as a choice on the list. Change your ddl code to this:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
<asp:ListItem>Please Select</asp:ListItem>
</asp:DropDownList>Wednesday, December 28, 2011 9:11 AM -
User1459882081 posted
Could you show us some more code? Like how you fill your ddl?
Wednesday, December 28, 2011 9:17 AM -
User-1907931422 posted
You are right, but I want to assign dt.Rows[0]["Organization"].ToString() to DropDownList1.SelectedValue directly why no lucky?
I added
<asp:ListItem>Please Select</asp:ListItem>
Now only one item "Please Select" in the dropdown menu.
More code:SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MembershipDB"].ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "SELECT [FirstName], [LastName], [Degree], [Organization], [Phone],[Ext] FROM [User_ExtraInfo] WHERE [UserID] = @UserID"; string UserID = user.ProviderUserKey.ToString(); cmd.Parameters.Add("@UserID", SqlDbType.VarChar).Value = UserID; DataTable dt = new DataTable(); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); } TextFirstName.Text = dt.Rows[0]["FirstName"].ToString(); TextLastName.Text = dt.Rows[0]["LastName"].ToString(); TextDegree.Text = dt.Rows[0]["Degree"].ToString(); if (dt.Rows[0]["Organization"].ToString() == null || dt.Rows[0]["Organization"].ToString() == "") DropDownList1.SelectedValue = "Please Select"; else DropDownList1.SelectedValue = dt.Rows[0]["Organization"].ToString(); TextPhone.Text = dt.Rows[0]["Phone"].ToString(); TextExt.Text = dt.Rows[0]["Ext"].ToString();
Wednesday, December 28, 2011 9:19 AM -
User1459882081 posted
You can only select the value that already is existing in the ddl. First make sure how you populate the ddl and it has the value you want to select.
Wednesday, December 28, 2011 9:24 AM -
User2053451246 posted
else
DropDownList1.Items.Add(dt.Rows[0]["Organization"].ToString()) <-- add this line
DropDownList1.SelectedValue = dt.Rows[0]["Organization"].ToString();- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 28, 2011 9:25 AM -
User1459882081 posted
Is it working now?
Wednesday, December 28, 2011 9:27 AM -
User-1907931422 posted
It works out, let me double check.
Wednesday, December 28, 2011 9:33 AM -
User-1907931422 posted
It only add the first row, how can I add all rows? I removed where clause in the command.
In my code I used Rows[0], there are other rows such as Rows[1], Rows[2]....etc.
Wednesday, December 28, 2011 9:45 AM -
User1459882081 posted
Ideally you should use this...
ddl.Items.Clear(); ddl.Items.Add(new ListItem("<Select an item>", "-1")); ddl.AppendDataBoundItems = true; ddl.DataSource = //Assign your datatable here
ddl.DataTextField = "What you want to display in ddl"; ddl.DataValueField = "ID for the item"; ddl.DataBind();
Wednesday, December 28, 2011 9:49 AM -
User2053451246 posted
Put inside a loop:
DataRow dr = null; foreach (DataRow dr in dt.Rows()) { DropDownList1.Items.Add(dr["Organization"].ToString()); }
I'm a VB programmer. Hopefully my code converted to C# correctly for you. :)- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 28, 2011 9:53 AM -
User-1907931422 posted
I mean that can I use foreach? But how to extract the specific column?
foreach(item in dt.Rows) { DropDownList1.Items.Add(item) }
Wednesday, December 28, 2011 9:54 AM -
User1459882081 posted
Like...
item["Organization"].ToString();
But ideally you should not need to use loop.
Wednesday, December 28, 2011 9:56 AM