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

Question
-
User-90830185 posted
OK here's my problem:
I have a dropdownlist named "cboGrpLeader" which is populated from the SQL Data Source object where DataTextField="ldgGrpLeader" and DataValueField="ldgGrpLeader" .
The following code assigned text from GridView called "gvLeafletDist" to cboGrpLeader as follows:
cboTGrpLeader.Text = (TryCast(gvLeafletDist.SelectedRow.FindControl("lblGrpLeader"), Label)).Text
All the above worked fine.
I then changed the dropdownlist to contain the primary key item as follows:
DataTextField="ldgGrpLeader" and DataValueField="ldgID" .
Now, the code to assign the text from gvLeafletDist (above) gives an error:
'cboTGrpLeader' has a SelectedValue which is invalid because it does not exist in the list of items.
Would appreciate if someone can assist please?
Saturday, January 17, 2015 6:49 AM
Answers
-
User-90830185 posted
No, the SQL is fine, I have found the fix as follows:
First I have added a hidden filed in my GridView as follows:
<asp:TemplateField HeaderText="Group Leader ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblGrpLeaderID" runat="server" Text='<%#Eval("ldgID")%>' />
</ItemTemplate>
</asp:TemplateField>And then after reading, the Key filed and the Text field I have assigned the Key to the Hidden field (above) as follows:
cboTGroupLeader.Text = (TryCast(gvLeafletDist.SelectedRow.FindControl("lblGrpLeaderID"), Label)).Text
Because cbOTGroupLeader contains both the key and the text so it takes the key value and displays its text.
Thanks for the Help.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, January 18, 2015 2:51 PM
All replies
-
User-1506965535 posted
Show us the code of gridview. Here is a sample demp of populatiing value from the sql into table:-
<asp:DropDownList ID="ddlNgoName" runat="server" CssClass="selectpicker form-control-drp wd" Style="width: 100%" AutoPostBack="false" ValidationGroup="AddNew"></asp:DropDownList> --Code behind -- protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Binddropdownlist(); } } private void Binddropdownlist() { SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["yourconnectionstring"].ConnectionString); SqlCommand cmd = new SqlCommand("Select * from tbl_ngoname", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); ddlEventsNgo.DataValueField = ds.Tables[0].Columns["Column ID"].ToString(); ddlEventsNgo.DataTextField = ds.Tables[0].Columns["Column Name"].ToString(); ddlEventsNgo.DataSource = ds.Tables[0]; ddlEventsNgo.DataBind(); ddlEventsNgo.Items.Insert(0, new ListItem("--Select--", "0")); }
Saturday, January 17, 2015 7:08 AM -
User-90830185 posted
OK Let me explain again BTW I do NOT know c# I code in VB.
Here's the code for populating dropdownlist:
<asp:Label ID="Label5" runat="server" Text="Group Leader: " CssClass="labelStyle"></asp:Label>
<asp:DropDownList ID="cboTGrpLeader" runat="server" DataSourceID="SqlDataSource3" DataTextField="ldgGrpLeader" DataValueField="ldgGrpLeader" style="width:240px;"
AppendDataBoundItems="True">
<asp:ListItem Value="-1">-- Select a Group Leader--</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:LeafletDistributionConnectionString %>" SelectCommand="SELECT DISTINCT , ldgGrpLeader from tabLFTGroups GROUP BY ldgGrpLeader ORDER BY ldgGrpLeader"></asp:SqlDataSource>So let's say table tabLFTGroups contains the values (below) and so the Dropdownlist shows these as follows:
Item A
Item B
Item C
Item DThen the items are shown in the GridView:
<asp:TemplateField HeaderText="Group Leader" ItemStyle-Width="18%" ItemStyle-HorizontalAlign="left">
<ItemTemplate>
<asp:Label ID="lblGrpLeader" runat="server" Text='<%#Eval("ldgGrpLeader")%>' />
</ItemTemplate>
</asp:TemplateField>So they appear in the GrdiView as follows:
select Item A
select Item B
select Item C
select Item Dwhere "select" is the link to selected row in the GridView. When the user selects this link (e.g. first one containing value "Item A") the code below assign the text to the cboGrpLeader Dropdownlist text box as follows:
cboTGrpLeader.Text = (TryCast(gvLeafletDist.SelectedRow.FindControl("lblGrpLeader"), Label)).Text
So all above works fine and the cboGrpLeader shows "Item A" in its box.
However, when I change the Dropdownlist code as follows:
<asp:Label ID="Label5" runat="server" Text="Group Leader: " CssClass="labelStyle"></asp:Label>
<asp:DropDownList ID="cboTGrpLeader" runat="server" DataSourceID="SqlDataSource3" DataTextField="ldgGrpLeader" DataValueField="ldgID" style="width:240px;"
AppendDataBoundItems="True">
<asp:ListItem Value="-1">-- Select a Group Leader--</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:LeafletDistributionConnectionString %>" SelectCommand="SELECT DISTINCT ldgID, ldgGrpLeader from tabLFTGroups GROUP BY ldgID, ldgGrpLeader ORDER BY ldgGrpLeader"></asp:SqlDataSource>i.e. DataValueField is now equal to "ldgID" instead of "ldgGrpLeader" and when the user clicks the "select" button on GridView, I get an error -
'cboTGrpLeader' has a SelectedValue which is invalid because it does not exist in the list of...
Saturday, January 17, 2015 7:29 AM -
User-1506965535 posted
I think there is some mistake in your query. I suggest you to run the query using sql server. Below
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:LeafletDistributionConnectionString %>" SelectCommand="SELECT DISTINCT ldgID, ldgGrpLeader from tabLFTGroups GROUP BY ldgID, ldgGrpLeader ORDER BY ldgGrpLeader"></asp:SqlDataSource>Saturday, January 17, 2015 7:40 AM -
User-90830185 posted
No, the SQL is fine, I have found the fix as follows:
First I have added a hidden filed in my GridView as follows:
<asp:TemplateField HeaderText="Group Leader ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblGrpLeaderID" runat="server" Text='<%#Eval("ldgID")%>' />
</ItemTemplate>
</asp:TemplateField>And then after reading, the Key filed and the Text field I have assigned the Key to the Hidden field (above) as follows:
cboTGroupLeader.Text = (TryCast(gvLeafletDist.SelectedRow.FindControl("lblGrpLeaderID"), Label)).Text
Because cbOTGroupLeader contains both the key and the text so it takes the key value and displays its text.
Thanks for the Help.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, January 18, 2015 2:51 PM -
User-1506965535 posted
So if your issue is resolved you can mark the answer and complete the thread.
Monday, January 19, 2015 1:06 AM