views:

47

answers:

1

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_RowDeleting" 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.

A: 

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.

marapet