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

  • Question

  • User-1797368610 posted


    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 ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <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">
                    <legend style="font-weight: bold">Vendor form</legend>
                    <br />
                    <table border="0" cellpadding="5">
                            <col width="100" />
                            <col width="500" />
                        <tr class="FormContainer" style="display: none;">
                                <asp:TextBox ID="Txtvendorid" runat="server" Width="10px" ReadOnly="True"></asp:TextBox>
                        <tr class="FormContainer">
                            <td>Vendor Contacts:
                            <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>Phone number</asp:TableHeaderCell>
                                <br />
                                    <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" />--%>
                                <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();
                                <asp:Label ID="lblex_Role" runat="server" ForeColor="Red" />
                        <tr class="FormContainer">
                            <td>Description of Service:
                                <telerik:RadTextBox RenderMode="Lightweight" runat="server" ID="txt_ServiceDesc" Width="400px" EmptyMessage="Enter description" TextMode="MultiLine" Height="100px" Resize="None">
                                <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>--%>
                        <tr class="FormContainer">
                            <td>Contact Rep:
                            <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">
                                <br />
                                    <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="" />--%>
                                <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();
                            <td style="width: 250px">
                                <telerik:RadButton ID="btn_VendorDetails" runat="server" Width="150px" Text="Submit" OnClick="btn_VendorDetails_Click" AutoPostBack="true"/>
                            <td style="text-align: center">
                                <asp:Label ID="lblMsg" runat="server" ForeColor="#248f24"></asp:Label>
                                <asp:Label ID="lblError" runat="server" ForeColor="Red" />
                                <asp:Label ID="Lblexception" runat="server" Width="200px" ForeColor="#CC3300"></asp:Label>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server"></telerik:RadAjaxLoadingPanel>
    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()
                    ' 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")
                    Dim tb2 As TextBox = New TextBox()
                    tb2.ID = "Email" & i.ToString()
                    Dim tc2 As TableCell = New TableCell()
                    ' 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")
                    Dim tb3 As TextBox = New TextBox()
                    tb3.ID = "Address" & i.ToString()
                    Dim tc3 As TableCell = New TableCell()
                    Dim rfv3 As RequiredFieldValidator = New RequiredFieldValidator()
                    rfv3.ErrorMessage = "Address required"
                    rfv3.ControlToValidate = tb3.ID
                    rfv3.ForeColor = Color.Red
                    rfv3.Style.Add("font-size", "10px")
                    Dim tb4 As TextBox = New TextBox()
                    tb4.ID = "Phone_number" & i.ToString()
                    Dim tc4 As TableCell = New TableCell()
                    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")
                    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()
                    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")
            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()
            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
                    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!"
                    da = comm.ExecuteScalar()
                    tb1.Text = ""
                    tb2.Text = ""
                    tb3.Text = ""
                    tb4.Text = ""
                    tb5.Text = "Select"
                    tb6.Text = ""
                    txt_ServiceDesc.Text = ""
                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.


    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