locked
Dynamically adding rows data are not saving into database the way i want RRS feed

  • Question

  • User-1797368610 posted

    Hi,

    i am adding rows dynamically for Vendor contacts and as well Contacts Rep in my code. so that users can add multiple contacts for Vendor as well for Contact Rep. sometimes may be they will need to add just one vendor contact but multiple contact Rep. in that case what i need to change in my code to allow them add rows as they want and save them into database just clicking submit button? please have a look at my code, you will see there are two button "Add row event" to add rows dynamically.  i noticed if i add same number of vendor contacts and contact rep then al info saving into database but if i need to add one vendor contact and three contact rep then it just saving one contact rep that means it depends on how many rows adding for vendor contacts. but that shouldn't be the case, users should be able to add rows info as they want. hope you understand my requirement. 

    Thanks in advanced

    <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPage.Master" CodeBehind="Test.aspx.vb" Inherits="ITSoft_Maint_UI.Test2" %>
    
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <link href="styles/default.css" rel="stylesheet" />
        <script lang="JavaScript" src="scripts/ManAgt.js" type="text/javascript"></script>
        <script lang="javaScript" src="scripts/ManAgt_alert.js"></script>
        <script src="scripts/scripts.js"></script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
        <telerik:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" DecorationZoneID="wrapper" />
        <div id="wrapper" style="padding-top: 20px">
            <telerik:RadAjaxPanel ID="RadAjaxPanel" runat="server" CssClass="demo-container" LoadingPanelID="RadAjaxLoadingPanel">
                <fieldset>
                    <legend style="font-weight: bold">Vendor form</legend>
                    <br />
                    <table border="0" cellpadding="5">
                        <colgroup>
                            <col width="100" />
                            <col width="500" />
                        </colgroup>
                        <tr class="FormContainer" style="display: none;">
                            <td>
                                <asp:TextBox ID="Txtvendorid" runat="server" Width="10px" ReadOnly="True"></asp:TextBox>
                            </td>
                        </tr>
                        <tr class="FormContainer">
                            <td>Vendor Contacts:
                            </td>
                            <td style="text-align: left">
                                <asp:HiddenField ID="HiddenField1" Value="0" runat="server" />
                                <asp:Table ID="VendorContacts_Table" runat="server" border="1" CellPadding="5" CellSpacing="10" Style="border-collapse: collapse" Visible="True" GridLines="Both" ValidateRequestMode="Enabled">
                                    <asp:TableHeaderRow BackColor="Black" ForeColor="Silver" HorizontalAlign="Center">
                                        <asp:TableHeaderCell>Name</asp:TableHeaderCell>
                                        <asp:TableHeaderCell>Email</asp:TableHeaderCell>
                                        <asp:TableHeaderCell>Address</asp:TableHeaderCell>
                                        <asp:TableHeaderCell>Phone number</asp:TableHeaderCell>
                                        <asp:TableHeaderCell>Role</asp:TableHeaderCell>
                                    </asp:TableHeaderRow>
                                </asp:Table>
                                <br />
                                <div>
                                    <asp:Button ID="btn_AddVendors" runat="server" Text="add a row" OnClientClick="btn_AddVendors_Click()" />
    
                                    <%--<asp:Button ID="btn_Submit" runat="server" Text="Submit" OnClick="btn_Submit_Click1" />--%>
                                </div>
                                <asp:PlaceHolder runat="server">
                                    <script type="text/jscript">
                                        function btn_AddVendors_Click() {
                                            document.getElementById("<%= HiddenField1.ClientID%>").value = (parseInt(document.getElementById("<%= HiddenField1.ClientID%>").value) + 1).toString();
                                        }
                                    </script>
                                </asp:PlaceHolder>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblex_Role" runat="server" ForeColor="Red" />
                            </td>
                        </tr>
                        <tr class="FormContainer">
                            <td>Description of Service:
                            </td>
                            <td>
                                <telerik:RadTextBox RenderMode="Lightweight" runat="server" ID="txt_ServiceDesc" Width="400px" EmptyMessage="Enter description" TextMode="MultiLine" Height="100px" Resize="None">
                                </telerik:RadTextBox>
                                <asp:RequiredFieldValidator ID="NumercTextBoxRequiredFieldValidator" runat="server"
                                    Display="Dynamic" ControlToValidate="txt_ServiceDesc" ErrorMessage="*" ForeColor="red"></asp:RequiredFieldValidator>
                                <%--<asp:RangeValidator ID="NumericTextBoxRangeValidator" runat="server" ControlToValidate="RadNumericTextBox1"
                                    ErrorMessage="Age should be between 0 and 50" Display="Dynamic"
                                    MaximumValue="50" MinimumValue="0" Type="Integer"></asp:RangeValidator>--%>
                            </td>
                        </tr>                    
                        <tr class="FormContainer">
                            <td>Contact Rep:
                            </td>
                            <td style="text-align: left">
                                <asp:HiddenField ID="HiddenField2" Value="0" runat="server" />
                                <asp:Table ID="HDCContacts_Table" runat="server" border="1" CellPadding="5" CellSpacing="10" Style="border-collapse: collapse" Visible="True" GridLines="Both" ValidateRequestMode="Enabled">
                                    <asp:TableHeaderRow BackColor="Black" ForeColor="Silver" HorizontalAlign="Center">
                                        <asp:TableHeaderCell>Contact_Name</asp:TableHeaderCell>
                                    </asp:TableHeaderRow>
                                </asp:Table>
                                <br />
                                <div>
                                    <asp:Button ID="btn_HdcContacts" runat="server" Text="add a row" OnClientClick="btn_HdcContacts_Click()" />
    
                                    <%--<asp:Button ID="btn_Submit" runat="server" Text="Submit" OnClick="" />--%>
                                </div>
                                <asp:PlaceHolder runat="server">
                                    <script type="text/jscript">
                                        function btn_HdcContacts_Click() {
                                            document.getElementById("<%= HiddenField2.ClientID%>").value = (parseInt(document.getElementById("<%= HiddenField2.ClientID%>").value) + 1).toString();
                                        }
                                    </script>
                                </asp:PlaceHolder>
                            </td>
    
                        </tr>                    
                        <tr>
                            <td></td>
                        </tr>
    
                        <tr>
                            <td style="width: 250px">
                                <telerik:RadButton ID="btn_VendorDetails" runat="server" Width="150px" Text="Submit" OnClick="btn_VendorDetails_Click" AutoPostBack="true"/>
                            </td>
                            <td style="text-align: center">
                                <asp:Label ID="lblMsg" runat="server" ForeColor="#248f24"></asp:Label>
                                <asp:Label ID="lblError" runat="server" ForeColor="Red" />
                            </td>
                        </tr>
    
    
    
                        <%--<tr>
                            <td>
                                <asp:Label ID="Lblexception" runat="server" Width="200px" ForeColor="#CC3300"></asp:Label>
                            </td>
                        </tr>--%>
                    </table>
                </fieldset>
            </telerik:RadAjaxPanel>
        </div>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server"></telerik:RadAjaxLoadingPanel>
    </asp:Content>
    
    
    Imports System
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Web
    Imports System.Web.SessionState
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.HtmlControls
    Imports System.Data.Odbc
    Public Class Test2
        Inherits System.Web.UI.Page
        Private rows As Integer = 0
        Private rows2 As Integer = 0
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim Query As String       
            If HiddenField1.Value <> "" Then
                rows = Integer.Parse(HiddenField1.Value)
    
                For i As Integer = 0 To rows - 1
                    Dim tr As TableRow = New TableRow()
                    Dim tc As TableCell = New TableCell()
                    Dim tb As TextBox = New TextBox()
                    tb.ID = "Name" & i.ToString()
                    tc.Controls.Add(tb)
    
                    ' add Required Field validator in Cell 1
                    Dim rfv As RequiredFieldValidator = New RequiredFieldValidator()
                    rfv.ErrorMessage = "Name required"
                    rfv.ControlToValidate = tb.ID
                    rfv.ForeColor = Color.Red
                    rfv.Style.Add("font-size", "10px")
                    tc.Controls.Add(rfv)
    
                    Dim tb2 As TextBox = New TextBox()
                    tb2.ID = "Email" & i.ToString()
                    Dim tc2 As TableCell = New TableCell()
                    tc2.Controls.Add(tb2)
    
                    ' add Required Field validator AND Regular Expression validator in Cell 2
                    Dim rfv2 As RequiredFieldValidator = New RequiredFieldValidator()
                    rfv2.ErrorMessage = "Email required"
                    rfv2.ControlToValidate = tb2.ID
                    rfv2.ForeColor = Color.Red
                    rfv2.Style.Add("font-size", "10px")
                    tc2.Controls.Add(rfv2)
    
                    Dim tb3 As TextBox = New TextBox()
                    tb3.ID = "Address" & i.ToString()
                    Dim tc3 As TableCell = New TableCell()
                    tc3.Controls.Add(tb3)
                    Dim rfv3 As RequiredFieldValidator = New RequiredFieldValidator()
                    rfv3.ErrorMessage = "Address required"
                    rfv3.ControlToValidate = tb3.ID
                    rfv3.ForeColor = Color.Red
                    rfv3.Style.Add("font-size", "10px")
                    tc3.Controls.Add(rfv3)
                    Dim tb4 As TextBox = New TextBox()
                    tb4.ID = "Phone_number" & i.ToString()
                    Dim tc4 As TableCell = New TableCell()
                    tc4.Controls.Add(tb4)
    
                    Dim rfv4 As RequiredFieldValidator = New RequiredFieldValidator()
                    rfv4.ErrorMessage = "Phone Number required"
                    rfv4.ControlToValidate = tb4.ID
                    rfv4.ForeColor = Color.Red
                    rfv4.Style.Add("font-size", "10px")
                    tc4.Controls.Add(rfv4)
    
                    Dim tb5 As DropDownList = New DropDownList()
                    tb5.ID = "Role" & i.ToString()
                    tb5.Items.Add(New ListItem("Select", "Select"))
                    tb5.Items.Add(New ListItem("Account Manager", "Account Manager"))
                    tb5.Items.Add(New ListItem("Executive", "Executive"))
                    tb5.Items.Add(New ListItem("Sales", "Sales"))
                    tb5.Items.Add(New ListItem("Support", "Support"))
                    Dim tc5 As TableCell = New TableCell()
                    tc5.Controls.Add(tb5)
                    Dim rfv5 As RequiredFieldValidator = New RequiredFieldValidator()
                    rfv5.InitialValue = "Select"
                    rfv5.ErrorMessage = "Role required"
                    rfv5.ControlToValidate = tb5.ID
                    rfv5.ForeColor = Color.Red
                    rfv5.Style.Add("font-size", "10px")
                    tc5.Controls.Add(rfv5)
    
                    tr.Cells.Add(tc)
                    tr.Cells.Add(tc2)
                    tr.Cells.Add(tc3)
                    tr.Cells.Add(tc4)
                    tr.Cells.Add(tc5)
                    VendorContacts_Table.Rows.Add(tr)
                Next
            End If
            If HiddenField2.Value <> "" Then
                rows2 = Integer.Parse(HiddenField2.Value)
    
                For i As Integer = 0 To rows2 - 1
                    Dim tr As TableRow = New TableRow()
                    Dim tc As TableCell = New TableCell()
                    Dim tb As TextBox = New TextBox()
                    tb.ID = "Contact_Name" & i.ToString()
                    tc.Controls.Add(tb)
                    tr.Cells.Add(tc)
                    HDCContacts_Table.Rows.Add(tr)
                Next
            End If
        End Sub
    
        Protected Sub btn_VendorDetails_Click(sender As Object, e As EventArgs)
            Dim conn As OdbcConnection
            Dim comm As OdbcCommand
            Dim da As OdbcDataAdapter
            Dim connectionString As String
            Dim query, alert As String
    
            If IsValid() Then
                connectionString = "DSN=Test1; UID=Test; PWD=Test;"
                conn = New OdbcConnection(connectionString)
    
                query = "insert into VENDOR (Name,Email,Address,Phone_Number,Role,Service_Description,HDC_Contact,Creation_Date) 
                        values (:name,:email,:address,:phone_number,:role,:service_description,:hdc_contact,sysdate)"
                comm = New OdbcCommand(query, conn)
    
                For i As Integer = 0 To rows - 1
                    comm.Parameters.Clear()
                    Dim tb1 As TextBox = TryCast(VendorContacts_Table.Rows(i).Cells(0).FindControl("name" & i.ToString()), TextBox)
                    Dim tb2 As TextBox = TryCast(VendorContacts_Table.Rows(i).Cells(1).FindControl("email" & i.ToString()), TextBox)
                    Dim tb3 As TextBox = TryCast(VendorContacts_Table.Rows(i).Cells(2).FindControl("address" & i.ToString()), TextBox)
                    Dim tb4 As TextBox = TryCast(VendorContacts_Table.Rows(i).Cells(3).FindControl("Phone_number" & i.ToString()), TextBox)
                    Dim tb5 As DropDownList = TryCast(VendorContacts_Table.Rows(i).Cells(4).FindControl("role" & i.ToString()), DropDownList)
                    Dim tb6 As TextBox = TryCast(VendorContacts_Table.Rows(i).Cells(0).FindControl("contact_name" & i.ToString()), TextBox)
    
                    If tb5.SelectedItem.Text = "Select" Then
                        lblex_Role.Text = "Select Role from Drop Down. You cannot use Select"
                        alert = "<script language='javascript'>" &
                   "alert ('Select the project to use. You cannot use -none- to enter data.'); " &
                    "</script>" '
                        ClientScript.RegisterStartupScript(GetType(Page), "ClientScript", alert)
                        Exit Sub
                    End If
    
    
                    comm.Parameters.AddWithValue("@vendor_id", Txtvendorid.Text)
                    comm.Parameters.AddWithValue("@name", tb1.Text)
                    comm.Parameters.AddWithValue("@email", tb2.Text)
                    comm.Parameters.AddWithValue("@address", tb3.Text)
                    comm.Parameters.AddWithValue("@Phone_number", tb4.Text)
                    comm.Parameters.AddWithValue("@role", tb5.SelectedItem.Text)
                    comm.Parameters.AddWithValue("@service_description", txt_ServiceDesc.Text)
                    comm.Parameters.AddWithValue("@hdc_contact", tb6.Text)
    
                    lblMsg.Text = "Data entered successfully!"
    
                    conn.Open()
                    da = comm.ExecuteScalar()
                    conn.Close()
    
                    tb1.Text = ""
                    tb2.Text = ""
                    tb3.Text = ""
                    tb4.Text = ""
                    tb5.Text = "Select"
                    tb6.Text = ""
                    txt_ServiceDesc.Text = ""
    
                Next
            Else
                lblError.Text = "Validation Failed."
            End If
        End Sub
    
    End Class

    Friday, March 19, 2021 5:11 PM

All replies

  • User475983607 posted

    You are not using the GridView as intended which is part of reason you're having troubles.  GridView's bind to datasets to generate rows dynamically.  You're trying to do the opposite.

    I recommend following Web Forms standards and create a form where the user can add as many vendors are they like one record at a time.  This pattern is covered in any GridView tutorial.

    https://www.google.com/search?q=gridview+crud+operations+in+asp.net

    Keep in mind, Telerik is a paid for framework.  Telerik support is a far better option if you help with Telerik controls.

    Friday, March 19, 2021 5:57 PM
  • User-1797368610 posted

    Thanks for your reply. is there any way i could achieve my requirement utilizing my code? if not could you please provide me code/example how can i achieve my requirement? so that it will dynamically add rows for vendor contacts as well rows for Contacts Rep from two grid view and then save into database?

    Appreciated for your help.

    Monday, March 22, 2021 4:57 PM