0

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!

Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152
  • Have you tried running that `SelectCommand` in the `SqlDataSourceClasses` `SqlDataSource` in Query Analyzer to see if it works there? – Code Maverick Apr 04 '13 at 18:19
  • Thanks for the comment. I ran the queries in the SQL Server Management Studio to make sure they are ok. The data from the queries do show up when the user clicks on the DropDownLists. Update mode is where we are having the problem and I also made sure that there were parameters for the ClassID as well. – Emad-ud-deen Apr 04 '13 at 18:41
  • The only differences I can tell from the code posted is that in the working example, both the `EditItemTemplate` and `InsertItemTemplate` have the same `DropDownList` whereas the non-working `InsertItemTemplate`, you have `AppendDataBoundItems="True"` and `OnDataBinding=DropDownListClassInsert_DataBinding`. – Code Maverick Apr 04 '13 at 19:17
  • Thanks for the reply. Still not working after removing AppendDataBoundItems and renaming the ID's to DropDownListClass so maybe I will try and delete the coding and re-typing it back in. – Emad-ud-deen Apr 05 '13 at 16:04
  • You could also create a separate project with _just_ the Class `DetailsView`. Then once you get that working, add in the Student to see if it conflicts somehow. – Code Maverick Apr 05 '13 at 16:13

0 Answers0