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

Question
-
User905819550 posted
Hi All,
I want to get the data from this dropdown and display in a column name Category and add in the value into db column name remark, can anyone help me. It wont display in my data table..
And i didnt add anything in my vb code for data bind..
<asp:DropDownList ID="poptxtRemark" runat="server" Enabled="true" SelectedValue='<%# Bind("remark")%>' DataValueField="remark" DataTextField="Category">
<asp:ListItem>Action Taken</asp:ListItem>
<asp:ListItem>Late Action Taken</asp:ListItem>
<asp:ListItem>Process Interaction</asp:ListItem>
<asp:ListItem>Machine Start-up</asp:ListItem>
<asp:ListItem>Incoming Issue</asp:ListItem>
<asp:ListItem>Others</asp:ListItem>
<asp:ListItem>Pending For Analysis</asp:ListItem>
<asp:ListItem>Continuous Trigger From Last WW</asp:ListItem>
</asp:DropDownList>Tuesday, June 11, 2019 4:04 AM
All replies
-
User409696431 posted
Your question is a bit confusing. If you have a hard-coded list of select items in your DropDownList, there is no "DataValueField" or "DataTextField". Those are for when the DropDownList is bound to a data source to determine its select values, and tells it which column of that source to use for the value and which to use for the displayed text of its select items. It has nothing to do with putting values back into a data source.
If you want to put a selected text from the DropDownList into your data, you need to fetch the selected text in code behind, typically in the "OnSelectedIndexChanged" event of the DropDownList, and put that value into your data. It is not clear from your post if that is intended to add a new value to a table, or update data already in the table, but whichever, you would need to write the code to do that.
https://www.aspsnippets.com/Articles/ASPNet-DropDownList-SelectedIndexChanged-event-example-in-C-and-VBNet.aspx gives an example of fetching values from a DropDownList. The example simply displays an alert with the value. You'd instead use the value in code to update your data.
Wednesday, June 12, 2019 3:39 AM -
User288213138 posted
Hi hafifiw,
I am also very confused, have you only posted part of your code?
Bind() can only be used in the context of a databound control, but you only have one DropDownList control.
I made a demo with GridView, you can use it as a reference.
The code:
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="remark" HeaderText="remark" SortExpression="remark" /> <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" /> <asp:TemplateField HeaderText="DropDownList"> <ItemTemplate> <asp:DropDownList ID="poptxtRemark" runat="server" DataSourceID="SqlDataSource2" AppendDataBoundItems="true" DataTextField="Category" DataValueField="remark" SelectedValue='<%# Bind("remark")%>'> <asp:ListItem>Action Taken</asp:ListItem> <asp:ListItem>Late Action Taken</asp:ListItem> <asp:ListItem>Process Interaction</asp:ListItem> <asp:ListItem>Machine Start-up</asp:ListItem> <asp:ListItem>Incoming Issue</asp:ListItem> <asp:ListItem>Others</asp:ListItem> <asp:ListItem>Pending For Analysis</asp:ListItem> <asp:ListItem>Continuous Trigger From Last WW</asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT [remark], [Category] FROM [Test9]"></asp:SqlDataSource> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT [remark], [Category] FROM [Test9]"></asp:SqlDataSource> </div>
The result:
Best regards,
Sam
Wednesday, June 12, 2019 6:57 AM