locked
insert x records in the database with the same content but only different date RRS feed

  • Question

  • User855106052 posted

    Hello everyone, I have the following form:

                <div>
                    Name:&nbsp;&nbsp;&nbsp;
                    <asp:TextBox ID="TbxName" runat="server"></asp:TextBox><br />
                    <br />
    
                    Options:
                    <asp:TextBox ID="TbxOptions" runat="server" Text='88'></asp:TextBox>
    
                    <hr />
                    Date:<br />
                    <br />
                    <asp:TextBox ID="TbxDate1" runat="server"></asp:TextBox>
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
    
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                </div>
    
                <asp:TextBox ID="txtCounter" Visible="false" runat="server"></asp:TextBox>

    https://image.ibb.co/eRXXcQ/save.gif

    and microsoft access database db.mdb

    table: TabNames

    fields: Id, Name, Options, Date

    The idea would be an insert sql command to insert form information and I am using the following:

    			Con = New OleDbConnection(StrCon)
    			Con.Open()
    			Cmd = New OleDbCommand("TabNames", Con)
    			Cmd.CommandText = "INSERT INTO TabNames" + _
                              "(Name, Options, Date) " + _
                              "VALUES(" + _
    						    "'" + TbxName.Text.ToString() + "'" _
    						  + "," + _
    						    "'" + TbxOptions.Text.ToString() + "'" _
    						  + "," + _
    						    "'" + TbxDate.Text.ToString() + "'" _
    						  + ")"
    			Cmd.ExecuteNonQuery()
    			Con.Close()
    	
     
    
    
    

    works nice on single date.

    The question is how to get the other dates of text boxes and create new inserts.

    in the previous example, we fill out the form with Name = Jonh Doe, Options = Something here and the dates for example 23 24 and 25-08-2017, so it will create 3 entries in table:

    Id  Name        Options                Date

    Jonh Doe   Something here     23-08-2017

    Jonh Doe   Something here     24-08-2017

    Jonh Doe   Something here     25-08-2017

    what I have to change in my code?

    Thanks

    Monday, October 8, 2018 1:01 AM

Answers

  • User283571144 posted

    Hi indesk,

    Since I don't see your "btnAdd_Click" event codes, I created it by myself.

    You could created use viewstate to store the number of the created textbox.

    Then you could write loop logic to get each textboxs text.

    More details, you could refer to below codes:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestForLoop.aspx.vb" Inherits="VBWebform.TestForLoop" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
     
                <div>
                    Name:&nbsp;&nbsp;&nbsp;
                    <asp:TextBox ID="TbxName" runat="server"></asp:TextBox><br />
                    <br />
    
                    Options:
                    <asp:TextBox ID="TbxOptions" runat="server" Text='88'></asp:TextBox>
    
                    <hr />
                    Date:<br />
                    <br />
                    <asp:TextBox ID="TbxDate" runat="server"></asp:TextBox>
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
    
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>
                </div>
    
                <asp:TextBox ID="txtCounter" Visible="false" runat="server"></asp:TextBox>
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

    Imports System.Data.OleDb
    
    Public Class TestForLoop
        Inherits System.Web.UI.Page
    
    
        Protected Property NumberOfControls As Integer
            Get
                Return CInt(ViewState("NumControls"))
            End Get
            Set(ByVal value As Integer)
                ViewState("NumControls") = value
            End Set
        End Property
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                Me.NumberOfControls = 0
            Else
                Me.createControls()
            End If
        End Sub
    
        Private Sub createControls()
            Dim count As Integer = Me.NumberOfControls
    
            For i As Integer = 0 To count - 1
                Dim tx As TextBox = New TextBox()
                tx.ID = "TbxDate" & i.ToString()
                form1.Controls.Add(tx)
            Next
        End Sub
    
        Protected Sub btnAdd_Click(sender As Object, e As EventArgs)
            Dim tx As TextBox = New TextBox()
            tx.ID = "TbxDate" & NumberOfControls.ToString()
            form1.Controls.Add(tx)
            Me.NumberOfControls += 1
        End Sub
    
        Protected Sub btnSave_Click(sender As Object, e As EventArgs)
            Dim count As Integer = Me.NumberOfControls
            Dim StrCon = ""
    
            Dim Con As OleDbConnection = New OleDbConnection(StrCon)
            Con.Open()
            Dim Cmd As OleDbCommand = New OleDbCommand("TabNames", Con)
            Cmd.CommandText = "INSERT INTO TabNames" +
                              "(Name, Options, Date) " +
                              "VALUES(" +
                                "'" + TbxName.Text.ToString() + "'" _
                              + "," +
                                "'" + TbxOptions.Text.ToString() + "'" _
                              + "," +
                                "'" + TbxDate.Text.ToString() + "'" _
                              + ")"
            Cmd.ExecuteNonQuery()
    
            For i As Integer = 0 To count - 1
                Dim t1 As TextBox = CType(form1.FindControl("TbxDate" & i.ToString()), TextBox)
                Cmd.CommandText = "INSERT INTO TabNames" +
                      "(Name, Options, Date) " +
                      "VALUES(" +
                        "'" + TbxName.Text.ToString() + "'" _
                      + "," +
                        "'" + TbxOptions.Text.ToString() + "'" _
                      + "," +
                        "'" + t1.Text.ToString() + "'" _
                      + ")"
                Cmd.ExecuteNonQuery()
            Next
    
            Con.Close()
        End Sub
    End Class

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 8, 2018 7:00 AM

All replies

  • User283571144 posted

    Hi indesk,

    Since I don't see your "btnAdd_Click" event codes, I created it by myself.

    You could created use viewstate to store the number of the created textbox.

    Then you could write loop logic to get each textboxs text.

    More details, you could refer to below codes:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestForLoop.aspx.vb" Inherits="VBWebform.TestForLoop" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
     
                <div>
                    Name:&nbsp;&nbsp;&nbsp;
                    <asp:TextBox ID="TbxName" runat="server"></asp:TextBox><br />
                    <br />
    
                    Options:
                    <asp:TextBox ID="TbxOptions" runat="server" Text='88'></asp:TextBox>
    
                    <hr />
                    Date:<br />
                    <br />
                    <asp:TextBox ID="TbxDate" runat="server"></asp:TextBox>
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
    
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>
                </div>
    
                <asp:TextBox ID="txtCounter" Visible="false" runat="server"></asp:TextBox>
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

    Imports System.Data.OleDb
    
    Public Class TestForLoop
        Inherits System.Web.UI.Page
    
    
        Protected Property NumberOfControls As Integer
            Get
                Return CInt(ViewState("NumControls"))
            End Get
            Set(ByVal value As Integer)
                ViewState("NumControls") = value
            End Set
        End Property
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                Me.NumberOfControls = 0
            Else
                Me.createControls()
            End If
        End Sub
    
        Private Sub createControls()
            Dim count As Integer = Me.NumberOfControls
    
            For i As Integer = 0 To count - 1
                Dim tx As TextBox = New TextBox()
                tx.ID = "TbxDate" & i.ToString()
                form1.Controls.Add(tx)
            Next
        End Sub
    
        Protected Sub btnAdd_Click(sender As Object, e As EventArgs)
            Dim tx As TextBox = New TextBox()
            tx.ID = "TbxDate" & NumberOfControls.ToString()
            form1.Controls.Add(tx)
            Me.NumberOfControls += 1
        End Sub
    
        Protected Sub btnSave_Click(sender As Object, e As EventArgs)
            Dim count As Integer = Me.NumberOfControls
            Dim StrCon = ""
    
            Dim Con As OleDbConnection = New OleDbConnection(StrCon)
            Con.Open()
            Dim Cmd As OleDbCommand = New OleDbCommand("TabNames", Con)
            Cmd.CommandText = "INSERT INTO TabNames" +
                              "(Name, Options, Date) " +
                              "VALUES(" +
                                "'" + TbxName.Text.ToString() + "'" _
                              + "," +
                                "'" + TbxOptions.Text.ToString() + "'" _
                              + "," +
                                "'" + TbxDate.Text.ToString() + "'" _
                              + ")"
            Cmd.ExecuteNonQuery()
    
            For i As Integer = 0 To count - 1
                Dim t1 As TextBox = CType(form1.FindControl("TbxDate" & i.ToString()), TextBox)
                Cmd.CommandText = "INSERT INTO TabNames" +
                      "(Name, Options, Date) " +
                      "VALUES(" +
                        "'" + TbxName.Text.ToString() + "'" _
                      + "," +
                        "'" + TbxOptions.Text.ToString() + "'" _
                      + "," +
                        "'" + t1.Text.ToString() + "'" _
                      + ")"
                Cmd.ExecuteNonQuery()
            Next
    
            Con.Close()
        End Sub
    End Class

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 8, 2018 7:00 AM
  • User855106052 posted

    Very nice :)

    that's exactly what I need.

    Now one question please. I don't use asp:textbox but html input because I use a javascript calendar:

    <input type="text" id="TbxDate" runat="server"  />
        <!-- calendar -->  
     <link rel="stylesheet" href="styles/calendar/jquery-ui.css">
      <script src="styles/calendar/jquery-1.12.4.js"></script>
      <script src="styles/calendar/jquery-ui.js"></script>
      <script>
    	  $( function() {
    		$( "#TbxData" ).datepicker();
    	  } ); 
    
      </script>

    so I transform the code to:

        Dim Con As OleDbConnection = New OleDbConnection(StrCon)
            Con.Open()
            Dim Cmd As OleDbCommand = New OleDbCommand("TabNames", Con)
            Cmd.CommandText = "INSERT INTO TabNames" +
                              "(Name, Options, Date) " +
                              "VALUES(" +
                                "'" + TbxName.Text.ToString() + "'" _
                              + "," +
                                "'" + TbxOptions.Text.ToString() + "'" _
                              + "," +
                                "'" + TbxDate.Value() + "'" _
                              + ")"
            Cmd.ExecuteNonQuery()

    and it's fine but when I click in ADD button, it will add a asp.textboxes and not inputs. So I need the ADD button create inputs because I need the calendar picker it's possible?

    Sunday, October 14, 2018 10:22 AM
  • User475983607 posted

    Still you have not posted the relevant "add" code.  My best guess is the dynamically added inputs are missing a name attribute.  Also, runat="server" runs at the server and has no meaning in browser. 

    Basic example

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JsHtml.aspx.cs" Inherits="WebFormsApp.JsHtml" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.3.1.js"></script>
        <script src="Scripts/jquery-ui-1.12.1.js"></script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input id="Add" type="button" value="Add" />
                <asp:Button ID="Submit" runat="server" Text="Submit" OnClick="Submit_Click" />
            </div>
            <div id="dynamicInputs">
                <div><input type="text" name="TbxDate" class="datepicker" /></div>
            </div>
    
        </form>
        <script>
            //Init the datepicker for the initial input
            $(".datepicker").datepicker();
    
            //Add the dynamic input and assign the datepicker
            $('#Add').click(function (e) {
                e.preventDefault();
                AddDateInput();
            });
    
            function AddDateInput() {
                //Add the dynamic input and assign the datepicker
                $('#dynamicInputs div:last')
                    .after('<div><input type="text" name="TbxDate" class="datepicker" /></div>')
                    $(".datepicker").datepicker();
            }
        </script>
    </body>
    </html>
    
    protected void Submit_Click(object sender, EventArgs e)
    {
        //Inputs with the same name are submitted as a comma separated string
        string[] dates = Request["TbxDate"].Split(',');
    }

    Sunday, October 14, 2018 11:25 AM
  • User855106052 posted

    hello

    all my code to reference:

    <%@ Page Language="vb" AutoEventWireup="false" %>
    <%@ Import Namespace="System.Globalization" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.OleDb" %>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Linq" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>
    <%@ Import Namespace="System.Collections" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Import Namespace="System.Net.Mail" %>
    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="iTextSharp" %>
    <%@ Import Namespace="iTextSharp.text" %>
    <%@ Import Namespace="iTextSharp.text.pdf" %>
    <%@ Import Namespace="iTextSharp.text.html" %>
    <%@ Import Namespace="iTextSharp.text.html.simpleparser" %>
    <%@ Import Namespace="Microsoft.Reporting.WebForms" %>
    
    
    <script runat="server">
        Dim StrCon As String = ConfigurationSettings.AppSettings("strconexao")
        Dim Con As OleDbConnection
        Dim Cmd As OleDbCommand
        Dim Dtr As OleDbDataReader
        Dim Adpt as OleDbDataAdapter
        Dim Cmb As OleDbCommandBuilder
        Dim Dset As DataSet
        Dim Drw As Datarow
      
    
    
    
        Protected Property NumberOfControls As Integer
            Get
                Return CInt(ViewState("NumControls"))
            End Get
            Set(ByVal value As Integer)
                ViewState("NumControls") = value
            End Set
        End Property
    	
    	
    	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    		
            If Not Page.IsPostBack Then
                Me.NumberOfControls = 0
            Else
                Me.createControls()
            End If
        End Sub
    
        Private Sub createControls()
            Dim count As Integer = Me.NumberOfControls
    
            For i As Integer = 0 To count - 1
                Dim tx As TextBox = New TextBox()
                tx.ID = "TbxDate" & i.ToString()
                form1.Controls.Add(tx)
            Next
        End Sub
    	
    
        Protected Sub btnAdd_Click(sender As Object, e As EventArgs)
            Dim tx As TextBox = New TextBox()
            tx.ID = "TbxDate" & NumberOfControls.ToString()
            form1.Controls.Add(tx)
            Me.NumberOfControls += 1
        End Sub
    
    
        Protected Sub btnSave_Click(sender As Object, e As EventArgs)
            Dim count As Integer = Me.NumberOfControls
            'Dim StrCon = ""
    
            Con = New OleDbConnection(StrCon)
            Con.Open()
            Cmd = New OleDbCommand("TabLog", Con)
            Cmd.CommandText = "INSERT INTO TabLog" +
                              "(LogAcao, UtilizadorCod, LogDataHora) " +
                              "VALUES(" +
                                "'" + TbxName.Text.ToString() + "'" _
                              + "," +
                               "'" + TbxOptions.Text.ToString() + "'" _
                              + "," +
                                "'" + TbxDate.Value() + "'" _
                              + ")"
            Cmd.ExecuteNonQuery()
    
            For i As Integer = 0 To count - 1
                Dim t1 As TextBox = CType(form1.FindControl("TbxDate" & i.ToString()), TextBox)
                Cmd.CommandText = "INSERT INTO TabLog" +
                      "(LogAcao, UtilizadorCod, LogDataHora) " +
                      "VALUES(" +
                        "'" + TbxName.Text.ToString() + "'" _
                      + "," +
                       "'" + TbxOptions.Text.ToString() + "'" _
                      + "," +
                        "'" + t1.Text.ToString() + "'" _
                      + ")"
                Cmd.ExecuteNonQuery()
            Next
    
            Con.Close()
        End Sub
    
    
    
    
    
    
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>TPVService | Servi&ccedil;os</title>
    
        <!-- calendario -->  
     <link rel="stylesheet" href="estilos/calendario/jquery-ui.css">
      <script src="estilos/calendario/jquery-1.12.4.js"></script>
      <script src="estilos/calendario/jquery-ui.js"></script>
      <script>
    	  $( function() {
    		$( "#TbxDate" ).datepicker();
    	  } ); 
    	  
    	 
      </script>
    
      
      
    </head>
    
    <body  ng-app="firstApplication"  >
    <form runat="server" id="form1">
    
    <div>
                <div>
                    Name:&nbsp;&nbsp;&nbsp;
                    <asp:TextBox ID="TbxName" runat="server"></asp:TextBox><br />
                    <br />
    
                    Options:
                    <asp:TextBox ID="TbxOptions" runat="server" Text='2'></asp:TextBox>
    
                    <hr />
                    Date:<br />
                    <br />
                    <input type="text" class="TbxFormularios" id="TbxDate" style="width: 200px;margin-top:2px;" runat="server"  />
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
    
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                </div>
    
                <asp:TextBox ID="txtCounter" Visible="false" runat="server"></asp:TextBox>
            </div>
    
    
    </form>
    </body>
    </html>

    Sunday, October 14, 2018 1:35 PM