需要有关 asp 对象数据源的帮助

发布于 2024-09-06 10:48:07 字数 7497 浏览 2 评论 0原文

我有一个带有删除、插入方法的 Payment 类。

 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; }
}

选择

public List<Payment> GetPayments(string sortExpression, string sortDirection)

插入

public void InsertPayment(int userId, DateTime date, string fullname, string paymentType, string recordInfo, double sum)

更新

public void DeletePayment(int id)

,我在调用此方法时遇到了麻烦,

这是我的 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>

下一部分

<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>

'> '> '> '>   ' /> ' />

和我的 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" />

我遇到了这样的错误:

对象数据源“对象数据源1” 没有要插入的值。检查一下 'values' 字典包含 值。

抱歉我的大帖子) 需要帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-09-13 10:48:07

我刚刚遇到了这种模糊的错误,“检查‘值’字典是否包含值。”我花了一段时间才弄清楚这一点。我正在使用 Linq-to-Entities 并插入数据,我将我的 objectdatasource 连接到我创建的用于插入的类。当我收到此错误时,我将注意力集中在页面后面的代码上,认为我的插入方法存在问题。在使用 objectdatasource 的网格的 InsertCommand 中,我有这些代码行。

String applicationName = ((TextBox)((GridEditableItem)e.Item)["ApplicationName"].Controls[0]).Text;
dsSecurity.InsertParameters["ApplicationName"].DefaultValue = applicationName;
dsSecurity.Insert();

所有这些代码所做的只是从网格中的文本框中获取applicationName(我使用的是Telerik)并将其添加到对象数据源(dsSecurity)的参数集合中,然后执行插入。对此没有任何规定。那么问题出在哪里呢。

我的问题是数据库中有更多字段不能为空。除了 applicationName 之外,还有 LoweredApplicationName 和 applicationId 的 guid。我无法通过从 objectdatasource 调用的方法将此数据发送到数据库,并收到“没有要插入的值”错误。

如果我再次收到此错误,我将向下游查看数据库插入需要什么,并确保我发送了需要插入的所有字段。希望这有帮助。

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.

来日方长 2024-09-13 10:48:07

GridView 不支持开箱即用的插入(请参阅各种 文章 关于 互联网)。这意味着插入参数的值不会自动填充。

解决方案包括使用 ObjectDataSource 的 OnInserting 事件来填充插入参数的值。 geekswithblogs.net

在同一site,还有一个示例说明如何使用 EmptyDataTemplate 在 GridView 为空的情况下插入值(第一条记录)。

否则,Codeproject 托管一个使用插入功能扩展 GridView 的项目 - 这是一本关于 GridView 的好书,即使您决定不使用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文