On an ASP.Net web form, there are several TextBoxes and 2 DropDownLists. One of these DropDownLists is not working as expected. If the user changes the displayed value in the DropDownList, the changed value is not saved back to the database. The user can insert data into the database with the same DropDownList without any issues. The only way currently to change the value is to delete the row of data and re-inserting it with the new value from the DropDownList.
When the data is saved back to the database all changes to the other DropDownList and all other controls on the form are saved except for the "Class" DropDownList. Can you look at our coding and spot where we made an error?
This is the markup of the working DropDownList:
<asp:TemplateField HeaderText="Student:" SortExpression="StudentID">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownListStudent"
Runat="server"
DataSourceID="SqlDataSourceStudents"
DataTextField = "StudentName"
DataValueField="ID"
SelectedValue='<%# Bind("StudentID") %>'
ForeColor="Blue">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEditStudent" runat="server" ControlToValidate="DropDownListStudent"
ErrorMessage="Please select a Student here." Font-Bold="True" Font-Italic="True" ForeColor="Red"
SetFocusOnError="True" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList
ID="DropDownListStudent"
Runat="server"
DataSourceID="SqlDataSourceStudents"
DataTextField = "StudentName"
DataValueField="ID"
SelectedValue='<%# Bind("StudentID") %>'
ForeColor="Blue">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertStudent" runat="server" ControlToValidate="DropDownListStudent"
ErrorMessage="Please select a Student here." Font-Bold="True" Font-Italic="True" ForeColor="Red"
SetFocusOnError="True" Display="Dynamic">
</asp:RequiredFieldValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:DropDownList
ID="DropDownListStudent"
Runat="server"
DataSourceID="SqlDataSourceStudents"
DataTextField = "StudentName"
DataValueField="ID"
SelectedValue='<%# Bind("StudentID") %>'
Enabled="false"
ForeColor="Blue"
Font-Bold="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
This is the markup for the DropDownList that's not working:
<asp:TemplateField HeaderText="Class:" SortExpression="ClassID">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownListClassEdit"
Runat="server"
DataSourceID="SqlDataSourceClasses"
DataTextField = "ClassName"
DataValueField="ID"
SelectedValue='<%# Bind("ClassID") %>'
ForeColor="Blue">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEditClass" runat="server" ControlToValidate="DropDownListClassEdit"
ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red"
SetFocusOnError="True" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList
ID="DropDownListClassInsert"
Runat="server"
DataSourceID="SqlDataSourceClasses"
DataTextField = "ClassName"
DataValueField="ID"
SelectedValue='<%# Bind("ClassID") %>'
AppendDataBoundItems="True"
ForeColor="Blue"
OnDataBinding="DropDownListClassInsert_DataBinding">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertClass" runat="server" ControlToValidate="DropDownListClassInsert"
ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red"
SetFocusOnError="True" Display="Dynamic">
</asp:RequiredFieldValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:DropDownList
ID="DropDownListClass"
Runat="server"
DataSourceID="SqlDataSourceClasses"
DataTextField = "ClassName"
DataValueField="ID"
SelectedValue='<%# Bind("ClassID") %>'
Enabled="false"
ForeColor="Blue"
Font-Bold="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
This is the DataSource for the working DropDownList:
<asp:SqlDataSource
ID="SqlDataSourceStudents"
runat="server"
ConnectionString="<%$ ConnectionStrings:Knowledge Academy %>"
SelectCommand=
"SELECT NULL AS ID, NULL AS StudentName
UNION SELECT ID, Surname + ', ' + Forename AS StudentName
FROM Students
ORDER BY 2">
</asp:SqlDataSource>
This is the DataSource for the DropDownList that's not working:
<asp:SqlDataSource
ID="SqlDataSourceClasses"
runat="server"
ConnectionString="<%$ ConnectionStrings:Knowledge Academy %>"
SelectCommand=
"SELECT NULL AS ID, NULL AS ClassName, NULL AS Grade
UNION SELECT ID, ClassName + ' *** Grade: ' + Grade AS ClassName, Grade
FROM Classes
ORDER BY 2, 3">
</asp:SqlDataSource>
* Update *
I found the cause. I found out the hard way never place database table field names in DataKeyNames that are not part of the linking fields. I had ClassID in there like this:
<asp:DetailsView
ID="DetailsView"
runat="server"
AutoGenerateRows="False"
Height="50px"
Width="207px"
DataSourceID="SqlDataSourceDetails"
DataKeyNames="ID,ClassID"
OnItemCommand="DetailsViewDetails_ItemCommand">
I changed it to:
<asp:DetailsView
ID="DetailsView"
runat="server"
AutoGenerateRows="False"
Height="50px"
Width="207px"
DataSourceID="SqlDataSourceDetails"
DataKeyNames="ID"
OnItemCommand="DetailsViewDetails_ItemCommand">
And the issue went away!