Need help with asp objectdatasource
I've got a Payment class with delete, insert methods.
public class Payment
{
public int Id { get; set; }
public int UserId { get; set; }
public string Fullname { get; set; }
public DateTime Date { get; set; }
public double Sum { get; set; }
public string PaymentType { get; set; }
public string RecordInfo { get; set; }
}
select
public List<Payment> GetPayments(string sortExpression, string sortDirection)
insert
public void InsertPayment(int userId, DateTime date, string fullname, string paymentType, string recordInfo, double sum)
update
public void DeletePayment(int id)
and i got troubles with calling this methods
this is my gridview
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1" Width="900px" BackColor="White" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
AllowSorting="True" OnSorting="Sort" OnRowCommand="GridView1_RowCommand"
DataKeyNames="Id" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_R开发者_StackOverflow社区owDeleting" onrowupdated="GridView1_RowUpdated"
onrowupdating="GridView1_RowUpdating"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Fullname" SortExpression="Fullname" >
<EditItemTemplate>
<asp:TextBox ID="TextBox0" runat="server" Text='<%# Bind("Fullname") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="AddPayment" runat="server" CommandName="Insert" Text="Add" ValidationGroup="add" />
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Fullname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="Date">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Date", "{0:dd.MM.yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newDate" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="newDate"
ErrorMessage="Enter date" ValidationGroup="add" Display="Dynamic">*</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Date", "{0:dd.MM.yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
next part
<asp:TemplateField HeaderText="Sum" SortExpression="Sum" ItemStyle-HorizontalAlign="Right">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Sum","{0:0.00} грн") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newSum" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="newSum"
ErrorMessage="Enter sum" ValidationGroup="add" Display="Dynamic">*</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Sum","{0:0.00} грн") %>'></asp:Label>
</ItemTemplate>
'> '> '> '> ' /> ' />
and my objectDatasourse
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="CostsReportControl.Payment"
DeleteMethod="DeletePayment" InsertMethod="InsertPayment" SelectMethod="GetPayments"
TypeName="CostsReportControl.Payments" UpdateMethod="UpdatePayment">
<SelectParameters>
<asp:Parameter Direction="input" Type="string" Name="sortExpression" />
<asp:Parameter Direction="input" Type="string" Name="sortDirection" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="userId" Type="Int32" />
<asp:Parameter Name="date" Type="DateTime" />
<asp:Parameter Name="fullname" Type="String" />
<asp:Parameter Name="paymentType" Type="String" />
<asp:Parameter Name="recordInfo" Type="String" />
<asp:Parameter Name="sum" Type="Double" />
</InsertParameters>
</asp:ObjectDataSource>
<asp:ValidationSummary ID="ValidationSummary3" runat="server" ValidationGroup="add"
ShowMessageBox="True" ShowSummary="False" />
i got mistakes like this:
ObjectDataSource 'ObjectDataSource1' has no values to insert. Check that the 'values' dictionary contains values.
Sorry for my big post) Need help.
The GridView does not support inserts out of the box (see various articles on the internet). This means that the values of the insert parameters are not filled in automatically.
A solution consists of using the OnInserting
event of the ObjectDataSource to fill in the values of the insert parameters. There is a complete example on how to do that with a SqlDataSource on geekswithblogs.net
On the same site, there is also an example on how to use the EmptyDataTemplate to insert values in case the GridView is empty (first record).
Otherwise, Codeproject hosts a project which extends GridView with insert functionality - it's a good read about the GridView even if you decide not to use it.
I just ran into this sort of vague error, "Check that the 'values' dictionary contains values." and it took me a while to figure this out. I was using Linq-to-Entities and to insert the data I hooked up my objectdatasource to the class I created to do the insert. When I got this error I focused on the code behind page thinking there was a problem in my insert method. In my InsertCommand for the grid which was using the objectdatasource I had these lines of code.
String applicationName = ((TextBox)((GridEditableItem)e.Item)["ApplicationName"].Controls[0]).Text;
dsSecurity.InsertParameters["ApplicationName"].DefaultValue = applicationName;
dsSecurity.Insert();
All this code does is simply get the applicationName from the textbox in the grid (I am using Telerik) and add it to the parameter collection of the objectdatasource (dsSecurity) and then do an insert. Nothing compliated about this. So what is the problem.
My problem was that there were more fields in the database that could not be null. In addition to applicationName, there was LoweredApplicationName and a guid for applicationId. I failed to send this data to the database in the method I was calling from the objectdatasource and got the "no values to insert" error.
If I get this error again, I will look downstream at what the database insert needs and make sure I am sending all the fields over that need to be inserted. Hope this helps.
精彩评论