VB.NET (web) gridView sends delete command twice?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • myth0s
    New Member
    • Jan 2008
    • 32

    VB.NET (web) gridView sends delete command twice?

    Hello!

    In VB.NET I have a GridView that shows a list of dates and beside each date there's a delete button.

    For a reason I don't know, the delete event seems to be fired twice, but I have trouble finding the exact source of the problem.

    From this thread : http://bytes.com/forum/threadnav436365-1-10.html, I have set my command button as a template field and call "GridView1.Dele teRow(Int32.Par se(e.CommandArg ument.ToString( )))" when the button is clicked.

    Just above that line, I call a method that logs whatever I want : Logger("[DEBUG] ImageButton1_Co mmand"). In the log file, I see only one entry, as if the block of code is executed only once.

    But when I use SqlProfiler to see what's happening at the server's level, I see that my stored procedure is called twice because I have two following line two times :
    exec pln_add_excepti on @date='2008-09-07 00:00:00:000',@ planification_i d=9


    ......

    My VB code :

    Code:
    Protected Sub ImageButton1_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
            Logger("[DEBUG] ImageButton1_Command")
            GridView1.DeleteRow(Int32.Parse(e.CommandArgument.ToString()))
    End Sub
    
    
    Private Sub Logger(ByVal message As String)
            Dim logEntry As New LogEntry()
            logEntry.EventId = 100
            logEntry.Priority = 1
            logEntry.Title = "Logging message"
            logEntry.Message = message
            logEntry.Categories.Add("General")
            Logger.Write(logEntry)
    End Sub
    My GridView :
    Code:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlServerExecutionsPlanifiees" DataKeyNames="date" EnableViewState="False">
       <Columns>
          <asp:TemplateField HeaderText="date" SortExpression="date">
             <ItemTemplate>
                <asp:Label ID="dateLabel" runat="server" Text='<%# Eval("date", "{0:d}") %>' CssClass='<%# "planifDate" & CType(Eval("planifie"), String) %>'></asp:Label>
             </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField>
             <ItemTemplate>
                <asp:ImageButton runat="server" id="ImageButton1" CommandName="Delete" ImageUrl="images/delete.png" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' OnCommand="ImageButton1_Command" />
             </ItemTemplate>
          </asp:TemplateField>
       </Columns>
    </asp:GridView>
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    In the Sql Profiler, is one line referencing the begin process and one line referencing the end of process? One of my colleagues complained of this last week and he was reading the Sql Profiler wrong - turns out what he thought were "two calls" were in fact two records describing the begin and the end of the same process...

    Comment

    • myth0s
      New Member
      • Jan 2008
      • 32

      #3
      Originally posted by balabaster
      In the Sql Profiler, is one line referencing the begin process and one line referencing the end of process? One of my colleagues complained of this last week and he was reading the Sql Profiler wrong - turns out what he thought were "two calls" were in fact two records describing the begin and the end of the same process...
      I doubt it.

      There's the following line between the two calls :
      exec sp_reset_connec tion

      And each call says "RPC: Completed" and "Reads: 3" (the number of reads is good, but it should do so only one time).

      What I am trying to find right now is a way to know what happens "between" the Web App and SQL Server. If I could know where the problem comes from, that would helpful!!

      Comment

      • myth0s
        New Member
        • Jan 2008
        • 32

        #4
        I've added logging methods everywhere I could and :

        This is called once :
        Code:
        GridView1.DeleteRow(Int32.Parse(e.CommandArgument.ToString()))
        Code:
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ....

        But at the server side my stored procedure gets called twice :

        Code:
        ALTER PROCEDURE [production].[pln_ajouter_exception]
        @date AS SMALLDATETIME,
        @planification_id AS BIGINT
        AS
        ....
        
        INSERT INTO production.test (texte, temps)
        VALUES ('@nbException : ' + CAST(@nbException AS Nvarchar(max)), GETDATE())
        
        ...
        This is two records in in test table (the date is the same). There should be only one.

        EDIT : I may add that I do not fully understand how my grid view behaves. When I click on the delete button, the page is sent back to the server. I thought that the delete procedure (set in the grid view's propreties) get executed and then the page is rendered again (meaning that the select procedure is executed).

        What I see when I look in the SQL Profiler is this :
        Code:
        1. exec sp_executesql N'SELECT TOP 30  [date], planifie FROM plnv_tous_evenements WHERE ([id] = @id) ORDER BY date',N'@id bigint',@id=9
        2. exec sp_reset_connection 
        3. exec pln_ajouter_exception @date='2008-09-07 00:00:00:000',@planification_id=9
        4. exec sp_reset_connection 
        5. exec pln_ajouter_exception @date='2008-09-07 00:00:00:000',@planification_id=9
        6. exec sp_reset_connection
        7. exec sp_executesql N'SELECT TOP 30  [date], planifie FROM plnv_tous_evenements WHERE ([id] = @id) ORDER BY date',N'@id bigint',@id=9
        What is going on?? From my understanding, I should only see steps 5 to 7..... shouldn't I?

        Comment

        Working...