通过 ObjectDataSource 和 GridView 实现乐观并发的问题

发布于 2024-08-25 17:27:57 字数 6183 浏览 5 评论 0原文

我在 ASP .NET 2.0 应用程序中遇到问题。

我有一个 GridView 显示来自 ObjectDataSource 的数据(连接到 BLL 类,该类连接到 TabledAdapter (使用乐观并发的类型化数据集)。

选择(显示数据)工作得很好,但是,当我更新一行时,GridView 确实会通过 我有一种感觉,

    <DataObjectMethod(DataObjectMethodType.Update, True)> _
    Public Function UpdateOC(ByVal original_id As Integer, ByVal original_fotonummer As Integer, ByVal original_inhoud As String, ByVal original_postdatum As Date?, ByVal fotonummer As Integer, ByVal inhoud As String, ByVal postdatum As Date?) As Boolean
        Dim tweets As TwitpicOC.TweetsDataTable = adapterOC.GetTweetById(original_id)
        If tweets.Rows.Count = 0 Then Return False
        Dim row As TwitpicOC.TweetsRow = tweets(0)

        SmijtHetErIn(row, original_fotonummer, original_inhoud, original_postdatum)
        row.AcceptChanges()

        SmijtHetErIn(row, fotonummer, inhoud, postdatum)

        Return adapterOC.Update(row) = 1
    End Function

    Public Sub SmijtHetErIn(ByVal row As TwitpicOC.TweetsRow, ByVal original_fotonummer As Integer, ByVal original_inhoud As String, ByVal original_postdatum As Date?)
        With row
            .fotonummer = original_fotonummer
            If String.IsNullOrEmpty(original_inhoud) Then .SetinhoudNull() Else .inhoud = original_inhoud
            If Not original_postdatum.HasValue Then .SetpostdatumNull() Else .postdatum = original_postdatum.Value
        End With
    End Sub

这是页面的一部分:

<div id='Overzicht' class='post'>
    <div class='title'>
        <h2>
            <a href='javascript:;'>Tweetsoverzicht</a></h2>
        <p>
            Overzicht</p>
    </div>
    <div class='entry'>
        <p>
            <asp:ObjectDataSource ID="odsGebruiker" runat="server" OldValuesParameterFormatString=""
                SelectMethod="GetAll" TypeName="TakeHomeWeb.BLL.GebruikersBLL"></asp:ObjectDataSource>
            <asp:ObjectDataSource ID="odsFoto" runat="server" SelectMethod="GetFotosByGebruiker"
                TypeName="TakeHomeWeb.BLL.FotosBLL">
                <SelectParameters>
                    <asp:ControlParameter ControlID="ddlGebruiker" DefaultValue="0" Name="userid" PropertyName="SelectedValue"
                        Type="Int32" />
                </SelectParameters>
            </asp:ObjectDataSource>
            <form id="form1" runat="server">
            <asp:Label runat="server" AssociatedControlID="ddlGebruiker">Gebruiker:&nbsp;</asp:Label>
            <asp:DropDownList ID="ddlGebruiker" runat="server" AutoPostBack="True" DataSourceID="odsGebruiker"
                DataTextField="naam" DataValueField="userid" AppendDataBoundItems="True">
                <asp:ListItem Text="Kies een gebruiker" Value="-1" />
            </asp:DropDownList>
            <br />
            <asp:Label runat="server" AssociatedControlID="ddlFoto">Foto:&nbsp;</asp:Label>
            <asp:DropDownList ID="ddlFoto" runat="server" AutoPostBack="True" DataSourceID="odsFoto"
                DataTextField="url" DataValueField="id" AppendDataBoundItems="True">
                <asp:ListItem Value="-1">Kies een foto...</asp:ListItem>
            </asp:DropDownList>
            <br />
            <div style="float: left">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
                    DataSourceID="odsTweets">
                    <Columns>
                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                        <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                            SortExpression="id" />
                        <asp:BoundField DataField="fotonummer" HeaderText="fotonummer" SortExpression="fotonummer" />
                        <asp:BoundField DataField="inhoud" HeaderText="inhoud" SortExpression="inhoud" />
                        <asp:BoundField DataField="postdatum" HeaderText="postdatum" SortExpression="postdatum" />
                    </Columns>
                </asp:GridView>
                <asp:ObjectDataSource ID="odsTweets" runat="server" ConflictDetection="CompareAllValues"
                    DeleteMethod="DeleteOC" OldValuesParameterFormatString="original_{0}" SelectMethod="GetTweetsByFotoId"
                    TypeName="TakeHomeWeb.BLL.TweetsOCBLL" UpdateMethod="UpdateOC">
                    <DeleteParameters>
                        <asp:Parameter Name="original_id" Type="Int32" />
                        <asp:Parameter Name="original_fotonummer" Type="Int32" />
                        <asp:Parameter Name="original_inhoud" Type="String" />
                        <asp:Parameter Name="original_postdatum" Type="DateTime" />
                    </DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="original_id" Type="Int32" />
                        <asp:Parameter Name="original_fotonummer" Type="Int32" />
                        <asp:Parameter Name="original_inhoud" Type="String" />
                        <asp:Parameter Name="original_postdatum" Type="DateTime" />
                        <asp:Parameter Name="fotonummer" Type="Int32" />
                        <asp:Parameter Name="inhoud" Type="String" />
                        <asp:Parameter Name="postdatum" Type="DateTime" />
                    </UpdateParameters>
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ddlFoto" Name="foto" PropertyName="SelectedValue"
                            Type="Int32" />
                    </SelectParameters>
                </asp:ObjectDataSource>
            </div>
            </form>
        </p>
    </div>
</div>

其中涉及到巨大的失败,但我已经盯着它看了几个小时了,但我就是找不到它。

I'm having a problem in an ASP .NET 2.0 Application.

I have a GridView displaying data from an ObjectDataSource (connected to a BLL class which connects to a TabledAdapter (Typed Dataset using optimistic concurrency).

The select (displaying the data) works just fine, however, when I update a row the GridView does pass the old values to the ObjectDataSource.

    <DataObjectMethod(DataObjectMethodType.Update, True)> _
    Public Function UpdateOC(ByVal original_id As Integer, ByVal original_fotonummer As Integer, ByVal original_inhoud As String, ByVal original_postdatum As Date?, ByVal fotonummer As Integer, ByVal inhoud As String, ByVal postdatum As Date?) As Boolean
        Dim tweets As TwitpicOC.TweetsDataTable = adapterOC.GetTweetById(original_id)
        If tweets.Rows.Count = 0 Then Return False
        Dim row As TwitpicOC.TweetsRow = tweets(0)

        SmijtHetErIn(row, original_fotonummer, original_inhoud, original_postdatum)
        row.AcceptChanges()

        SmijtHetErIn(row, fotonummer, inhoud, postdatum)

        Return adapterOC.Update(row) = 1
    End Function

    Public Sub SmijtHetErIn(ByVal row As TwitpicOC.TweetsRow, ByVal original_fotonummer As Integer, ByVal original_inhoud As String, ByVal original_postdatum As Date?)
        With row
            .fotonummer = original_fotonummer
            If String.IsNullOrEmpty(original_inhoud) Then .SetinhoudNull() Else .inhoud = original_inhoud
            If Not original_postdatum.HasValue Then .SetpostdatumNull() Else .postdatum = original_postdatum.Value
        End With
    End Sub

And this is the part of the page:

<div id='Overzicht' class='post'>
    <div class='title'>
        <h2>
            <a href='javascript:;'>Tweetsoverzicht</a></h2>
        <p>
            Overzicht</p>
    </div>
    <div class='entry'>
        <p>
            <asp:ObjectDataSource ID="odsGebruiker" runat="server" OldValuesParameterFormatString=""
                SelectMethod="GetAll" TypeName="TakeHomeWeb.BLL.GebruikersBLL"></asp:ObjectDataSource>
            <asp:ObjectDataSource ID="odsFoto" runat="server" SelectMethod="GetFotosByGebruiker"
                TypeName="TakeHomeWeb.BLL.FotosBLL">
                <SelectParameters>
                    <asp:ControlParameter ControlID="ddlGebruiker" DefaultValue="0" Name="userid" PropertyName="SelectedValue"
                        Type="Int32" />
                </SelectParameters>
            </asp:ObjectDataSource>
            <form id="form1" runat="server">
            <asp:Label runat="server" AssociatedControlID="ddlGebruiker">Gebruiker: </asp:Label>
            <asp:DropDownList ID="ddlGebruiker" runat="server" AutoPostBack="True" DataSourceID="odsGebruiker"
                DataTextField="naam" DataValueField="userid" AppendDataBoundItems="True">
                <asp:ListItem Text="Kies een gebruiker" Value="-1" />
            </asp:DropDownList>
            <br />
            <asp:Label runat="server" AssociatedControlID="ddlFoto">Foto: </asp:Label>
            <asp:DropDownList ID="ddlFoto" runat="server" AutoPostBack="True" DataSourceID="odsFoto"
                DataTextField="url" DataValueField="id" AppendDataBoundItems="True">
                <asp:ListItem Value="-1">Kies een foto...</asp:ListItem>
            </asp:DropDownList>
            <br />
            <div style="float: left">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
                    DataSourceID="odsTweets">
                    <Columns>
                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                        <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                            SortExpression="id" />
                        <asp:BoundField DataField="fotonummer" HeaderText="fotonummer" SortExpression="fotonummer" />
                        <asp:BoundField DataField="inhoud" HeaderText="inhoud" SortExpression="inhoud" />
                        <asp:BoundField DataField="postdatum" HeaderText="postdatum" SortExpression="postdatum" />
                    </Columns>
                </asp:GridView>
                <asp:ObjectDataSource ID="odsTweets" runat="server" ConflictDetection="CompareAllValues"
                    DeleteMethod="DeleteOC" OldValuesParameterFormatString="original_{0}" SelectMethod="GetTweetsByFotoId"
                    TypeName="TakeHomeWeb.BLL.TweetsOCBLL" UpdateMethod="UpdateOC">
                    <DeleteParameters>
                        <asp:Parameter Name="original_id" Type="Int32" />
                        <asp:Parameter Name="original_fotonummer" Type="Int32" />
                        <asp:Parameter Name="original_inhoud" Type="String" />
                        <asp:Parameter Name="original_postdatum" Type="DateTime" />
                    </DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="original_id" Type="Int32" />
                        <asp:Parameter Name="original_fotonummer" Type="Int32" />
                        <asp:Parameter Name="original_inhoud" Type="String" />
                        <asp:Parameter Name="original_postdatum" Type="DateTime" />
                        <asp:Parameter Name="fotonummer" Type="Int32" />
                        <asp:Parameter Name="inhoud" Type="String" />
                        <asp:Parameter Name="postdatum" Type="DateTime" />
                    </UpdateParameters>
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ddlFoto" Name="foto" PropertyName="SelectedValue"
                            Type="Int32" />
                    </SelectParameters>
                </asp:ObjectDataSource>
            </div>
            </form>
        </p>
    </div>
</div>

I've got a feeling there's huge fail involved or something, but I've been staring at it for hours now and I just can't find it.

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

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

发布评论

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

评论(1

心意如水 2024-09-01 17:27:57

我遇到了完全相同的问题。
我试图让乐观并发发挥作用
所以我从删除一个有两列的表开始

,但根据本地窗口,
看起来 dataGrid 正在传递 null
对于主键以外的值。

无论如何,在到处调整和阅读之后,
这是我的问题的解决方案:

在 ObjectDataSource 上我修改了 1 个属性:
ConflictDetection =“CompareAllValues”

愚蠢的我..

I had the exact same problem.
I was trying to get optimistic concurrency to work
so i started from a deletion on a table with 2 columns

but according to the local window,
it seemed like the dataGrid is passing null
for values other than the primary key.

anyways after tweaking and reading here and there,
this was the solution to my problems:

on ObjectDataSource i modified 1 property:
ConflictDetection="CompareAllValues"

silly me..

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