chump2877
09-12-2007, 02:47 PM
OK ASP.NET experts, I've been struggling with a problem for awhile I need your help resolving it...
I've got a SQL-databound GridView control that is throwing an exception any time I try to update/delete a row...
on UPDATE:
You have specified that your update command compares all values on SqlDataSource 'dsStudents', but the dictionary passed in for oldValues is empty. Pass in a valid dictionary for update or change your mode to OverwriteChanges.
on DELETE:
You have specified that your delete command compares all values on SqlDataSource 'dsStudents', but the dictionary passed in for values is empty. Pass in a valid dictionary for delete or change your mode to OverwriteChanges.
I am completely stumped here, as my code looks OK to me...Huge thanks (and reps) will be given to anyone who can spot the error in my code! Here is my code:
Students.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Students.aspx.cs" Inherits="Week12.Students" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Student GridView</title>
<link type="text/css" rel="Stylesheet" href="Students.css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvStudents" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="StudentId" DataSourceID="dsStudents" ForeColor="#333333" GridLines="None" PageSize="5" ShowFooter="True" OnRowCommand="gvStudents_RowCommand" OnRowDeleted="gvStudents_RowDeleted" OnRowUpdated="gvStudents_RowUpdated">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="btnUpdateStudent" runat="server" CommandName="Update" Text="Update" ValidationGroup="Update" />
<asp:Button ID="btnCancelEdit" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnEditStudent" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="btnDeleteStudent" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddStudent" runat="server" CommandName="Insert" Text="Add Student" ValidationGroup="Add" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="StudentId">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("StudentId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("StudentId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbAddStudentId" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="ID is a required field." ControlToValidate="tbAddStudentId" SetFocusOnError="true" Display="None" ValidationGroup="Add"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="You entered an invalid ID." ControlToValidate="tbAddStudentId" SetFocusOnError="true" ValidationExpression="^[A-Z]{2}\d{4}$" Display="None" ValidationGroup="Add"></asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="tbEditFirstName" runat="server" Text='<%# Eval("FirstName") %>' MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="First Name is a required field." Display="None" SetFocusOnError="true" ControlToValidate="tbEditFirstName" ValidationGroup="Update"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbAddFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="First Name is a required field." Display="None" SetFocusOnError="true" ControlToValidate="tbAddFirstName" ValidationGroup="Add"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="tbEditLastName" runat="server" Text='<%# Eval("LastName") %>' MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Last Name is a required field." Display="None" SetFocusOnError="true" ControlToValidate="tbEditLastName" ValidationGroup="Update"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbAddLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Last Name is a required field." Display="None" SetFocusOnError="true" ControlToValidate="tbAddLastName" ValidationGroup="Add"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<div id="SummaryContainer"><asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList" ShowMessageBox="true" ShowSummary="true" HeaderText="Please correct the following errors:" ValidationGroup="Update" /><asp:ValidationSummary ID="ValidationSummary2" runat="server" DisplayMode="BulletList" ShowMessageBox="true" ShowSummary="true" HeaderText="Please correct the following errors:" ValidationGroup="Add" /></div>
<asp:SqlDataSource ID="dsStudents" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:TriCConnectionString %>" DeleteCommand="DELETE FROM [Student] WHERE [StudentId] = @original_StudentId AND [FirstName] = @original_FirstName AND [LastName] = @original_LastName"
InsertCommand="INSERT INTO [Student] ([StudentId], [FirstName], [LastName]) VALUES (@StudentId, @FirstName, @LastName)"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Student]"
UpdateCommand="UPDATE [Student] SET [FirstName] = @FirstName, [LastName] = @LastName WHERE [StudentId] = @original_StudentId AND [FirstName] = @original_FirstName AND [LastName] = @original_LastName">
<DeleteParameters>
<asp:Parameter Name="original_StudentId" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="original_StudentId" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="StudentId" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<p class="error"><asp:Label ID="lblError" runat="server"></asp:Label></p>
</div>
</form>
<script type="text/javascript" src="JavaScript2.js"></script>
</body>
</html>
There is a lot of code, so I'll split the code up into 2 posts....see next post for the rest of the code....
I've got a SQL-databound GridView control that is throwing an exception any time I try to update/delete a row...
on UPDATE:
You have specified that your update command compares all values on SqlDataSource 'dsStudents', but the dictionary passed in for oldValues is empty. Pass in a valid dictionary for update or change your mode to OverwriteChanges.
on DELETE:
You have specified that your delete command compares all values on SqlDataSource 'dsStudents', but the dictionary passed in for values is empty. Pass in a valid dictionary for delete or change your mode to OverwriteChanges.
I am completely stumped here, as my code looks OK to me...Huge thanks (and reps) will be given to anyone who can spot the error in my code! Here is my code:
Students.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Students.aspx.cs" Inherits="Week12.Students" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Student GridView</title>
<link type="text/css" rel="Stylesheet" href="Students.css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvStudents" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="StudentId" DataSourceID="dsStudents" ForeColor="#333333" GridLines="None" PageSize="5" ShowFooter="True" OnRowCommand="gvStudents_RowCommand" OnRowDeleted="gvStudents_RowDeleted" OnRowUpdated="gvStudents_RowUpdated">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="btnUpdateStudent" runat="server" CommandName="Update" Text="Update" ValidationGroup="Update" />
<asp:Button ID="btnCancelEdit" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnEditStudent" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="btnDeleteStudent" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddStudent" runat="server" CommandName="Insert" Text="Add Student" ValidationGroup="Add" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="StudentId">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("StudentId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("StudentId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbAddStudentId" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="ID is a required field." ControlToValidate="tbAddStudentId" SetFocusOnError="true" Display="None" ValidationGroup="Add"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="You entered an invalid ID." ControlToValidate="tbAddStudentId" SetFocusOnError="true" ValidationExpression="^[A-Z]{2}\d{4}$" Display="None" ValidationGroup="Add"></asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="tbEditFirstName" runat="server" Text='<%# Eval("FirstName") %>' MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="First Name is a required field." Display="None" SetFocusOnError="true" ControlToValidate="tbEditFirstName" ValidationGroup="Update"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbAddFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="First Name is a required field." Display="None" SetFocusOnError="true" ControlToValidate="tbAddFirstName" ValidationGroup="Add"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="tbEditLastName" runat="server" Text='<%# Eval("LastName") %>' MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Last Name is a required field." Display="None" SetFocusOnError="true" ControlToValidate="tbEditLastName" ValidationGroup="Update"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbAddLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Last Name is a required field." Display="None" SetFocusOnError="true" ControlToValidate="tbAddLastName" ValidationGroup="Add"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<div id="SummaryContainer"><asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList" ShowMessageBox="true" ShowSummary="true" HeaderText="Please correct the following errors:" ValidationGroup="Update" /><asp:ValidationSummary ID="ValidationSummary2" runat="server" DisplayMode="BulletList" ShowMessageBox="true" ShowSummary="true" HeaderText="Please correct the following errors:" ValidationGroup="Add" /></div>
<asp:SqlDataSource ID="dsStudents" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:TriCConnectionString %>" DeleteCommand="DELETE FROM [Student] WHERE [StudentId] = @original_StudentId AND [FirstName] = @original_FirstName AND [LastName] = @original_LastName"
InsertCommand="INSERT INTO [Student] ([StudentId], [FirstName], [LastName]) VALUES (@StudentId, @FirstName, @LastName)"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Student]"
UpdateCommand="UPDATE [Student] SET [FirstName] = @FirstName, [LastName] = @LastName WHERE [StudentId] = @original_StudentId AND [FirstName] = @original_FirstName AND [LastName] = @original_LastName">
<DeleteParameters>
<asp:Parameter Name="original_StudentId" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="original_StudentId" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="StudentId" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<p class="error"><asp:Label ID="lblError" runat="server"></asp:Label></p>
</div>
</form>
<script type="text/javascript" src="JavaScript2.js"></script>
</body>
</html>
There is a lot of code, so I'll split the code up into 2 posts....see next post for the rest of the code....