Reputation: 45
I have a asp.net gridview that is giving me fits. I'm pulling the data into the grid with no issues. However, when I click edit, I get this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'wrkCode'.
My intention is to allow the user to edit a group's work schedule using a dropdown list. Here is my code:
<asp:GridView ID="grdShowGroups" runat="server" datakeynames="grpID" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="5" GridLines="Vertical" CellSpacing="5" Width="700px" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="grdShowGroups_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="Edit" />
<asp:BoundField DataField="grpID" HeaderText="grpID" SortExpression="grpID" InsertVisible="False" ReadOnly="True" Visible="false" />
<asp:BoundField DataField="grpStartTime" HeaderText="Start Time" SortExpression="grpStartTime" />
<asp:BoundField DataField="grpEndTime" HeaderText="End Time" SortExpression="grpEndTime" />
<asp:TemplateField HeaderText="Work Schedule" SortExpression="wrkSchedule">
<EditItemTemplate>
<asp:DropDownList ID="drpWrkSchedule" runat="server" DataSourceID="SqlDataSource2" DataTextField="wrkDescription" DataValueField="wrkCode" SelectedValue='<%#Bind("wrkCode")%>' AppendDataBoundItems="true" AutoPostBack="true"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblWrkSchedule" runat="server" Text='<%# Bind("wrkSchedule") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="grpDescription" HeaderText="Description" SortExpression="grpDescription" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeMGTConnectionString %>" SelectCommand="SELECT [grpID], [grpStartTime], [grpEndTime], [wrkSchedule], [grpDescription] FROM [empGroups]" DeleteCommand="DELETE FROM [empGroups] WHERE [grpID] = @grpID" InsertCommand="INSERT INTO [empGroups] ([grpStartTime], [grpEndTime], [wrkSchedule], [grpDescription]) VALUES (@grpStartTime, @grpEndTime, @wrkSchedule, @grpDescription)" UpdateCommand="UPDATE [empGroups] SET [grpStartTime] = @grpStartTime, [grpEndTime] = @grpEndTime, [wrkSchedule] = @wrkSchedule, [grpDescription] = @grpDescription WHERE [grpID] = @grpID">
<DeleteParameters>
<asp:Parameter Name="grpID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter DbType="DateTime" Name="grpStartTime" />
<asp:Parameter DbType="DateTime" Name="grpEndTime" />
<asp:Parameter Name="wrkSchedule" Type="String" />
<asp:Parameter Name="grpDescription" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter DbType="DateTime" Name="grpStartTime" />
<asp:Parameter DbType="DateTime" Name="grpEndTime" />
<asp:Parameter Name="wrkSchedule" Type="String" />
<asp:Parameter Name="grpDescription" Type="String" />
<asp:Parameter Name="grpID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeMGTConnectionString %>" SelectCommand="SELECT [wrkCode], [wrkDescription], [wrkID] FROM [wrkSchedule]"></asp:SqlDataSource>
If I run it with this code for the dropdown list:
<asp:DropDownList ID="drpWrkSchedule" runat="server" DataSourceID="SqlDataSource2" DataTextField="wrkDescription" DataValueField="wrkCode" SelectedValue='<%#Bind("wrkCode")%>'>
I get the error. IF I run it without the "SelectedValue='<%#Bind("wrkCode")%>'" code, it presents the dropdown list as I need, just without a selected value that connects to the data from the data currently in the table.
I've looked at several examples from all over the web and it may be that I'm just missing something small. I just can't figure this out.
Thanks!
Upvotes: 1
Views: 600
Reputation: 2100
You need to select the wrkCode in your sqldatasource1 otherwise it has nothing to bind to. So just include wrkCode in your select clause of that sql statement and you should be good to go.
Upvotes: 1