locked
Dynamic Data Linq to SQL Web Application – no update occurs if parameter value the same RRS feed

  • Question

  • User-1484203585 posted

    I have created my first basic application using template ASP.NET Dynamic  Data Linq to SQL Web Application (Visual Basic) to learn about LINQ to SQL.

    The components are a Gridview, a DataContext model,  a stored procedure and a view.

    So far, the application is working as expected with the exception that if i try to update with the same rating as that previously entered it will not trigger an update.

    So, in the screenshot below for example, if I enter  9 again as a rating, the row will not change to Rating=9, Total_rating =18, Total_ratings=2  even if done after a delibarate error has been forced (see row 2 ) and then 9 re-entered.

    The code is essentially either default or auto-generated (as below).

    This concept has not been problematic before, when in another application I was using an SqlDataSource instead of a LinqDataSource ....

     UpdateCommand="Yes_Update" UpdateCommandType="StoredProcedure">
            <UpdateParameters>
                <asp:Parameter Name="ID" Type="Int32" />
                <asp:Parameter Name="Rating" Type="Int32" />
            </UpdateParameters>

    Any help would be appreciated, thanks.

    Sample screen shot

     

    Id

    First_Name

    Last_Name

    Instrument_Name

    Rating

    Total_rating

    Total_ratings

    Edit

    1

    Jon

    Anderson

    Lead Vocals

    9

    9

    1

    Update Cancel

    2

    Peter

    Banks

    Guitar

    <input value=" " textbox1="" ctl03="" gridview1="" contentplaceholder1="" type="text" />Rating cannot be left blank Enter integer between 0 and 10

    0

    0

     

    Default.aspx.vb Imports System.Web.DynamicData
    
    Class _Default
        Inherits Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
       
    
            Dim visibleTables As IList = Global_asax.DefaultModel.VisibleTables
            If visibleTables.Count = 0 Then
                Throw New InvalidOperationException("There are no accessible tables. " &
                    "Make sure that at least one data model is registered in Global.asax " &
                    "and scaffolding is enabled or implement custom pages.")
            End If
        
    
        End Sub
    End Class
    
    <View Join_1> SELECT     TOP (100) PERCENT myDB.yes_personnel.IdPerson, myDB.yes_personnel.First_Name, 
                          myDB.yes_personnel.Last_Name, myDB.yes_instruments.Instrument_Name, 
                          myDB.yes_ratings.Rating, myDB.yes_ratings.Total_rating, myDB.yes_ratings.Total_ratings
    FROM         myDB.yes_instruments INNER JOIN
                          myDB.yes_personnel ON 
                          myDB.yes_instruments.IdPerson = myDB.yes_personnel.IdPerson INNER JOIN
                          myDB.yes_ratings ON myDB.yes_personnel.IdPerson = myDB.yes_ratings.IdRating
    ORDER BY myDB.yes_personnel.IdPerson
    
    <Store Procedure Yes_Update>
    USE [myDB]
    GO
    /****** Object:  StoredProcedure [myDB].[Yes_Update]    Script Date: 07/15/2011 14:28:13 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:		
    -- Create date: 09.07.2011
    -- Description:	Yes Update
    -- =============================================
    ALTER PROCEDURE [myDB].[Yes_Update] 
    	-- Add the parameters for the stored procedure here
    	@ID int = 0, 
    	@Rating int = 0
    AS
    BEGIN
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
    
        -- Insert statements for procedure here
    	UPDATE myDB.yes_ratings
    	SET Rating = @Rating,
    		Total_rating = Total_rating + @Rating,
    		Total_ratings = Total_ratings + 1
    		WHERE
    		IdRating = @ID
    END
    
    The update occurs in YesLINQ.designer.vb which is auto-generated and here is the relevant code. <Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_Rating", DbType:="Int NOT NULL")>  _
    	Public Property Rating() As Integer
    		Get
    			Return Me._Rating
    		End Get
    		Set
    			If ((Me._Rating = value)  _
    						= false) Then
    				Me.OnRatingChanging(value)
    				Me.SendPropertyChanging
    				Me._Rating = value
    				Me.SendPropertyChanged("Rating")
    				Me.OnRatingChanged
    			End If
    		End Set
    	End Property
    <Global.System.Data.Linq.Mapping.FunctionAttribute(Name:="myDB.Yes_Update")>  _
    	Public Function Yes_Update(<Global.System.Data.Linq.Mapping.ParameterAttribute(Name:="ID", DbType:="Int")> ByVal iD As System.Nullable(Of Integer), <Global.System.Data.Linq.Mapping.ParameterAttribute(Name:="Rating", DbType:="Int")> ByVal rating As System.Nullable(Of Integer)) As Integer
    		Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod,MethodInfo), iD, rating)
    		Return CType(result.ReturnValue,Integer)
    	End Function
    
    Friday, July 15, 2011 10:53 AM

Answers

  • User-1484203585 posted

    Well, getting my inspiration from amongst other places http://msdn.microsoft.com/en-us/library/4x2877xb.aspx  (i.e Convert Type)

    I put in the following code in LinqDataSource1_Updating

      Dim originalYes = e.OriginalObject
            Dim updateableYes As Join_1 = CType(originalYes, Join_1)
            updateableYes.Rating = -1
            originalYes = updateableYes
    This has now resolved my problem in that I can force an update if the new parameter is the same as the original parameter by intercepting the original rating at source.
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 18, 2011 8:26 AM

All replies

  • User-1484203585 posted

    Here is the default.aspx source

    <%@ Page Language="VB" MasterPageFile="~/Site.master" CodeBehind="Default.aspx.vb" Inherits="LINQYes._Default" %>
    
    <asp:Content ID="headContent" ContentPlaceHolderID="head" Runat="Server">
    </asp:Content>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" />
    
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    
        <br /><br />
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="LinqDataSource1" AllowPaging="True" AllowSorting="True" 
            DataKeyNames="IdPerson">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="IdPerson" HeaderText="Id" ReadOnly="True" 
                    SortExpression="IdPerson" />
                <asp:BoundField DataField="First_Name" HeaderText="First_Name" ReadOnly="True" 
                    SortExpression="First_Name" />
                <asp:BoundField DataField="Last_Name" HeaderText="Last_Name" ReadOnly="True" 
                    SortExpression="Last_Name" />
                <asp:BoundField DataField="Instrument_Name" HeaderText="Instrument_Name" 
                    ReadOnly="True" SortExpression="Instrument_Name" />
                <asp:TemplateField HeaderText="Rating" SortExpression="Rating">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Height="19px" 
                            Text='<%# Bind("Rating") %>' Width="16px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                            ControlToValidate="TextBox1" Display="Dynamic" 
                            ErrorMessage="Rating cannot be left blank" ForeColor="Red"></asp:RequiredFieldValidator>
                        <asp:RangeValidator ID="RangeValidator1" runat="server" 
                            ControlToValidate="TextBox1" Display="Dynamic" 
                            ErrorMessage="Enter integer between 0 and 10" ForeColor="Red" MaximumValue="10" 
                            MinimumValue="0" ToolTip="Enter your rating here" Type="Integer"></asp:RangeValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Rating") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Total_rating" HeaderText="Total_rating" 
                    ReadOnly="True" SortExpression="Total_rating" />
                <asp:BoundField DataField="Total_ratings" HeaderText="Total_ratings" 
                    ReadOnly="True" SortExpression="Total_ratings" />
            </Columns>
        </asp:GridView>
        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
            ContextTypeName="LINQYes.YesLINQDataContext" EntityTypeName="" 
            TableName="Join_1s" EnableUpdate="True" OrderBy="IdPerson">
        </asp:LinqDataSource>
    </asp:Content>
    
    Friday, July 15, 2011 10:56 AM
  • User3866881 posted

    Hello:)
    What did you do with these events?

    1)   Me.OnRatingChanging(value)
    2)   Me.SendPropertyChanging
    3)   Me.SendPropertyChanged("Rating")
    4)   Me.OnRatingChanged

    Sunday, July 17, 2011 10:24 PM
  • User-1484203585 posted

    Thanks Decker.

    I have not amended these events as they are autogenerated in YesLINQ.designer.vb with the following caveat

    '------------------------------------------------------------------------------
    ' <auto-generated>
    '     This code was generated by a tool.
    '     Runtime Version:4.0.30319.235
    '
    '     Changes to this file may cause incorrect behavior and will be lost if
    '     the code is regenerated.
    ' </auto-generated>
    '------------------------------------------------------------------------------
    

    In debugging though I have noticed that in LinqDataSource1_Updating that {LINQYes_Join_1 } has properties NewObject and OriginalObject.

    I am thinking of perhaps trying to intercept the selected row and change the original rating to something such as minus 1, hopefully triggering the update.

    Monday, July 18, 2011 5:27 AM
  • User-1484203585 posted

    I have tried the following.

    In LinqDataSource1_Updating I added the statement

     Dim originalYes = e.OriginalObject

    This allowed me to amend Rating to minus 1 in debug mode (as represented below) and the update was triggered.

    How can I now convert this into a working line of code ?

    Thanks

     

    Name                    Value                                    Type

    originalYes          {LINQYes.Join_1}             Object

    LINQYes.Join_1 {LINQYes.Join_1}             LINQYes.Join_1

    _Rating                 -1                                            Integer

    Monday, July 18, 2011 7:38 AM
  • User-1484203585 posted

    Well, getting my inspiration from amongst other places http://msdn.microsoft.com/en-us/library/4x2877xb.aspx  (i.e Convert Type)

    I put in the following code in LinqDataSource1_Updating

      Dim originalYes = e.OriginalObject
            Dim updateableYes As Join_1 = CType(originalYes, Join_1)
            updateableYes.Rating = -1
            originalYes = updateableYes
    This has now resolved my problem in that I can force an update if the new parameter is the same as the original parameter by intercepting the original rating at source.
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 18, 2011 8:26 AM