Jun Rikson
Jun Rikson

Reputation: 1884

Duplicate Insert ASP.NET

This is make me crazy this last week. I have read many questions and answer regarding this problem but still can't fixed my problem. I have checked all possible event triggered and can't find any.

I have kondisi.aspx :

<!-- some header here -->

              <form id="form1" runat="server" class="form-horizontal">
                  <div class="box-body">
                      <div class="form-group">
                          <label class="col-sm-2 control-label text-right">Kondisi :</label>
                          <div class="col-sm-10">
                              <asp:textbox id="tbkondisi" runat="server" cssclass="form-control" textmode="MultiLine"></asp:textbox>
                          </div>
                      </div>

                      <asp:label id="lInfo" runat="server" cssclass="error" visible="False"></asp:label>
                      <asp:label id="linfoberhasil" runat="server" cssclass="berhasil" visible="False"></asp:label>

                      <div class="box-footer text-center">
                          <dxe:aspxbutton id="btSimpan" runat="server" text="Simpan" cssclass="btn btn-primary" enabledefaultappearance="False"></dxe:aspxbutton>
                          <dxe:aspxbutton id="btBatal" runat="server" text="Reset" cssclass="btn btn-danger" enabledefaultappearance="False"></dxe:aspxbutton>
                      </div>

                      <asp:hiddenfield id="hfID" runat="server" />
                      <asp:hiddenfield id="hfMode" runat="server" />

<!-- some grid and footer here -->

btSimpan click event which handled the button is :

Protected Sub btSimpan_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btSimpan.Click
    Try
        If Validasi() Then
            If hfMode.Value = "Insert" Then
                Insert()
                linfoberhasil.Visible = True
                linfoberhasil.Text = "Simpan Berhasil"
            Else
                Update(hfID.Value)
                linfoberhasil.Visible = True
                linfoberhasil.Text = "Update Berhasil"
            End If
        End If
    Catch ex As Exception
        Response.Write("<b>Error button simpan :</b>" & ex.ToString)
    End Try
End Sub

This is validation function which checking duplicate item :

Private Function Validasi() As Boolean
    Try
        clear_label()
        If hfMode.Value = "Insert" Then
            If tbkondisi.Text.Trim = "" Then
                lInfo.Visible = True
                lInfo.Text = "Nama kondisi masih kosong"
                Return False
            End If

            sqlstring = "SELECT ID FROM KondisiPengiriman " & _
                        "WHERE Nama_Kondisi = '" & tbkondisi.Text.Replace("'", "''") & "' " & _
                        "AND [status] = 1 "
            result = SQLExecuteScalar(sqlstring)

            If result <> "" Then
                lInfo.Visible = True
                lInfo.Text = "Nama kondisi sudah ada"
                Return False
            End If

        End If

        If hfMode.Value = "Update" Then
            If tbkondisi.Text.Trim = "" Then
                lInfo.Visible = True
                lInfo.Text = "Nama kondisi masih kosong"
                Return False
            End If
        End If


        Return True
    Catch ex As Exception
        Throw New Exception("Error function Validasi : " & ex.ToString)
    End Try
End Function

And here clear_label() function which reset all field :

Private Sub clear_label()
    lInfo.Visible = False
    lInfo.Text = ""
    linfoberhasil.Visible = False
    linfoberhasil.Text = ""
End Sub

And finally the insert() function :

Private Sub Insert()
    Try
        sqlstring = "INSERT INTO KondisiPengiriman " & _
                    "(Nama_Kondisi, UserName, [status]) VALUES " & _
                    "('" & tbkondisi.Text.Replace("'", "''") & "', '" & Session("UserId") & "', 1)"
        hasil = SQLExecuteNonQuery(sqlstring)

        If hasil > 0 Then
            load_grid_kondisi()
            clear()
        End If

    Catch ex As Exception
        Throw New Exception("<b>Error function insert :</b> " & ex.ToString)
    End Try
End Sub

Seems validation() just touched one time and it inserted 2 times, and this is how sql tracing while inserting :

RPC:Completed   exec sp_reset_connection    .Net SqlClient Data Provider        sa  0   0   0   0   15032   55  2017-06-13 10:36:28.910 2017-06-13 10:36:28.910 0X00000000000000002600730070005F00720065007300650074005F0063006F006E006E0065006300740069006F006E00      sa  ts11pc      TS11PC  
Audit Login -- network protocol: LPC
SQL:BatchStarting   SELECT ID FROM KondisiPengiriman WHERE Nama_Kondisi = 'tes' AND [status] = 1    .Net SqlClient Data Provider        sa                  15032   55  2017-06-13 10:36:28.910             sa  ts11pc      TS11PC  
SQL:BatchCompleted  SELECT ID FROM KondisiPengiriman WHERE Nama_Kondisi = 'tes' AND [status] = 1    .Net SqlClient Data Provider        sa  0   2   0   0   15032   55  2017-06-13 10:36:28.910 2017-06-13 10:36:28.910         sa  ts11pc      TS11PC  
Audit Logout        .Net SqlClient Data Provider        sa  0   1782    0   3   15032   55  2017-06-13 10:36:28.910 2017-06-13 10:36:28.913     1 - Non-DAC sa  ts11pc      TS11PC  
RPC:Completed   exec sp_reset_connection    .Net SqlClient Data Provider        sa  0   0   0   0   15032   55  2017-06-13 10:36:28.913 2017-06-13 10:36:28.913 0X00000000000000002600730070005F00720065007300650074005F0063006F006E006E0065006300740069006F006E00      sa  ts11pc      TS11PC  
Audit Login -- network protocol: LPC    
SQL:BatchStarting   INSERT INTO KondisiPengiriman (Nama_Kondisi, UserName, [status]) VALUES ('tes', 'LIGITAADMIN', 1)   .Net SqlClient Data Provider        sa                  15032   55  2017-06-13 10:36:28.913             sa  ts11pc      TS11PC  
SQL:BatchCompleted  INSERT INTO KondisiPengiriman (Nama_Kondisi, UserName, [status]) VALUES ('tes', 'LIGITAADMIN', 1)   .Net SqlClient Data Provider        sa  0   4   0   0   15032   55  2017-06-13 10:36:28.913 2017-06-13 10:36:28.913         sa  ts11pc      TS11PC  
Audit Logout        .Net SqlClient Data Provider        sa  0   1786    0   0   15032   55  2017-06-13 10:36:28.913 2017-06-13 10:36:28.913     1 - Non-DAC sa  ts11pc      TS11PC  
RPC:Completed   exec sp_reset_connection    .Net SqlClient Data Provider        sa  0   0   0   0   15032   55  2017-06-13 10:36:28.913 2017-06-13 10:36:28.913 0X00000000000000002600730070005F00720065007300650074005F0063006F006E006E0065006300740069006F006E00      sa  ts11pc      TS11PC  
Audit Login -- network protocol: LPC    
SQL:BatchStarting   INSERT INTO KondisiPengiriman (Nama_Kondisi, UserName, [status]) VALUES ('tes', 'LIGITAADMIN', 1)   .Net SqlClient Data Provider        sa                  15032   55  2017-06-13 10:36:28.913             sa  ts11pc      TS11PC  
SQL:BatchCompleted  INSERT INTO KondisiPengiriman (Nama_Kondisi, UserName, [status]) VALUES ('tes', 'LIGITAADMIN', 1)   .Net SqlClient Data Provider        sa  0   4   0   0   15032   55  2017-06-13 10:36:28.913 2017-06-13 10:36:28.913         sa  ts11pc      TS11PC  
Audit Logout        .Net SqlClient Data Provider        sa  0   1790    0   3   15032   55  2017-06-13 10:36:28.917 2017-06-13 10:36:28.920     1 - Non-DAC sa  ts11pc      TS11PC  
RPC:Completed   exec sp_reset_connection    .Net SqlClient Data Provider        sa  15  0   0   0   15032   55  2017-06-13 10:36:28.920 2017-06-13 10:36:28.920 0X00000000000000002600730070005F00720065007300650074005F0063006F006E006E0065006300740069006F006E00      sa  ts11pc      TS11PC  
Audit Login -- network protocol: LPC    
SQL:BatchStarting   SELECT ID, Nama_Kondisi from KondisiPengiriman where [status] = 1   .Net SqlClient Data Provider        sa                  15032   55  2017-06-13 10:36:28.920             sa  ts11pc      TS11PC  
SQL:BatchCompleted  SELECT ID, Nama_Kondisi from KondisiPengiriman where [status] = 1   .Net SqlClient Data Provider        sa  0   2   0   0   15032   55  2017-06-13 10:36:28.920 2017-06-13 10:36:28.920         sa  ts11pc      TS11PC  
Audit Logout        .Net SqlClient Data Provider        sa  0   1792    0   354220  15032   55  2017-06-13 10:36:28.920 2017-06-13 10:42:23.140     1 - Non-DAC sa  ts11pc      TS11PC  

I can't figure out what caused by this, seems the function just run 1 time but the insertion command 2 times.

Upvotes: 1

Views: 185

Answers (1)

Jun Rikson
Jun Rikson

Reputation: 1884

I'm sorry, I thought SQLExecuteNonQuery(sqlstring) is a built-in command. Obviously it is custom function which checked two instance name for backup server. And in settings, that 2 instance name pointing to same server, it what makes double insertion.

It is done now by removing one of instance name. Thank you.

Upvotes: 2

Related Questions