Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this button which i add in row of data however, i only add two row of the date, when click this button it give me 4 data, each date is being inserted twice meaning date A den date B den date A and den date B, what the problem


VB
Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click

      Dim rowIndex As Integer = 0
      Dim sc As New StringCollection()
      Dim sc1 As New Date
      If ViewState("CurrentTable") IsNot Nothing Then
          Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
          Dim drCurrentRow As DataRow = Nothing
          If dtCurrentTable.Rows.Count < 10 Then
              For i As Integer = 1 To dtCurrentTable.Rows.Count
                  'extract the TextBox values

                  Dim box5 As TextBox = DirectCast(Gridview3.Rows(rowIndex).Cells(1).FindControl("TextBox5"), TextBox)


                  Dim box7 As Date = Convert.ToDateTime(box5.Text)


                  Dim myConn As New SqlConnection
                  Dim myCmd As New SqlCommand
                  myConn.ConnectionString = ConfigurationManager.ConnectionStrings("mydatabase").ConnectionString
                  Dim cmd As String
                  cmd = "Insert into Date values (@date) "
                  myCmd.CommandText = cmd
                  myCmd.CommandType = CommandType.Text

                  myCmd.Parameters.Add(New SqlParameter("@date", box7))


                  myCmd.Connection = myConn
                  myConn.Open()
                  myCmd.ExecuteNonQuery()
                  myCmd.Dispose()
                  myConn.Dispose()
                  rowIndex += 1
              Next


          End If
      Else
          '   lblMessage.Text = "Cannot save as there no information recorded"
          MsgBox("failed")
      End If
  End Sub

ASP.NET
<asp:GridView ID="Gridview3" runat="server" AutoGenerateColumns="False" 
                Height="89px" ShowFooter="True" Width="303px">
                <Columns>
                    <asp:BoundField DataField="RowNumber" HeaderText="No of Available:" />
                    <asp:TemplateField HeaderText="New Date Available :">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox5" runat="server" />
                            <AjaxToolkit:CalendarExtender ID="calExtender6"  runat="server" 
                                Format="dd/MM/yyyy" OnClientDateSelectionChanged="CheckDateEalier" 
                                TargetControlID="TextBox5" />
                        </ItemTemplate>
                        <FooterStyle Height="22px" HorizontalAlign="Right" />
                        <FooterTemplate>
                            <asp:Button ID="ButtonAdd" runat="server" onclick="ButtonAdd_Click" 
                                Text="Add New Row" />
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView> 

HTML
Protected Sub ButtonAdd_Click(sender As Object, e As System.EventArgs)
      AddNewRowToGrid()
  End Sub
Posted

1 solution

Do the following. Set a break point where data is finally inserted. Test it under debugger. When a program stops on a break point, open Debug window "Call Stack". It will show you where each call comes from in each case. This way, you will find a source of a problem pretty soon.

Please, use Debugger before asking a question like that. And use Debugger every time you have a slightest concern about run-time behavior of your code.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900