[Solved] Dynamic TextBox value inserted zero in database using C# in ASP.Net

Last Reply 20 days ago By pandeyism

Posted 22 days ago

Hey, In below code whatever value i am inserting in txtbox it only stores zero(0) in data base.so please help me..aspx.cs

namespace Test_captcha
{
    public partial class test_table_dynamic : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            //c_email.Text = Session["Company"] as string;
        }

        protected void sprink_submit_Click(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("INSERT INTO test_sprink(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)", con);
            string[] textboxValues1 = Request.Form.GetValues("c_email");
            string[] textboxValues2 = Request.Form.GetValues("nozzle_lph");
            string[] textboxValues3 = Request.Form.GetValues("nozzle_price");
            string[] textboxValues4 = Request.Form.GetValues("txt_lat_mm");
            string[] textboxValues5 = Request.Form.GetValues("txt_lat_price");
            string[] textboxValues6 = Request.Form.GetValues("txt_smain_mm");
            string[] textboxValues7 = Request.Form.GetValues("txt_smain_price");
            string[] textboxValues8 = Request.Form.GetValues("txt_main_mm");
            string[] textboxValues9 = Request.Form.GetValues("txt_main_price");
            string[] textboxValues10 = Request.Form.GetValues("txt_mpum_hp");
            string[] textboxValues11 = Request.Form.GetValues("txt_mpump_price");


            int length = 0;
            if (textboxValues2.Length > length)
            {
                length = textboxValues2.Length;
            }
            else if (textboxValues3.Length > length)
            {
                length = textboxValues3.Length;
            }
            else if (textboxValues4.Length > length)
            {
                length = textboxValues4.Length;
            }
            else if (textboxValues5.Length > length)
            {
                length = textboxValues5.Length;
            }
            else if (textboxValues6.Length > length)
            {
                length = textboxValues6.Length;
            }
            else if (textboxValues7.Length > length)
            {
                length = textboxValues7.Length;
            }
            else if (textboxValues8.Length > length)
            {
                length = textboxValues8.Length;
            }
            else if (textboxValues9.Length > length)
            {
                length = textboxValues9.Length;
            }
            else if (textboxValues10.Length > length)
            {
                length = textboxValues10.Length;
            }
            else if (textboxValues11.Length > length)
            {
                length = textboxValues11.Length;
            }

            for (int i = 0; i < length; i++)
            {
                cmd = new SqlCommand("INSERT INTO test_sprink(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)", con);
                cmd.Parameters.AddWithValue("@company_email", textboxValues1[0]);
                cmd.Parameters.AddWithValue("@nozzle_lph", textboxValues2[i].Length < i ? textboxValues2[i] : "");
                cmd.Parameters.AddWithValue("@nozzle_price", textboxValues3[i].Length < i ? textboxValues3[i] : "");
                cmd.Parameters.AddWithValue("@lateral_mm", textboxValues4[i].Length < i ? textboxValues4[i] : "");
                cmd.Parameters.AddWithValue("@lateral_price", textboxValues5[i].Length < i ? textboxValues5[i] : "");
                cmd.Parameters.AddWithValue("@main_mm", textboxValues6[i].Length < i ? textboxValues6[i] : "");
                cmd.Parameters.AddWithValue("@main_price", textboxValues7[i].Length < i ? textboxValues7[i] : "");
                cmd.Parameters.AddWithValue("@submain_mm", textboxValues8[i].Length < i ? textboxValues8[i] : "");
                cmd.Parameters.AddWithValue("@submain_price", textboxValues9[i].Length < i ? textboxValues9[i] : "");
                cmd.Parameters.AddWithValue("@Sp_mpump_hp", textboxValues10[i].Length < i ? textboxValues10[i] : "");
                cmd.Parameters.AddWithValue("@Sp_mpump_price", textboxValues11[i].Length < i ? textboxValues11[i] : "");
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}

datatable

CREATE TABLE [dbo].[test_sprink] (
    [sprink_id]      INT           IDENTITY (1, 1) NOT NULL,
    [company_email]  VARCHAR (100) NOT NULL,
    [nozzle_lph]     INT           NOT NULL,
    [nozzle_price]   FLOAT (53)    NOT NULL,
    [lateral_mm]     INT           NOT NULL,
    [lateral_price]  FLOAT (53)    NOT NULL,
    [main_mm]        INT           NOT NULL,
    [main_price]     FLOAT (53)    NOT NULL,
    [submain_mm]     INT           NOT NULL,
    [submain_price]  FLOAT (53)    NOT NULL,
    [Sp_mpump_hp]    INT           NOT NULL,
    [Sp_mpump_price] FLOAT (53)    NOT NULL,
    PRIMARY KEY CLUSTERED ([sprink_id] ASC)
);

 

Posted 20 days ago

Hi nid@patel,

Refer below sample.

HTML

<script type="text/javascript">
    function addRow(tableID) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for (var i = 0; i < colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
            switch (newcell.childNodes[0].type) {
                case "text":
                    newcell.childNodes[0].value = "";
                    break;
                case "checkbox":
                    newcell.childNodes[0].checked = false;
                    break;
                case "select-one":
                    newcell.childNodes[0].selectedIndex = 0;
                    break;
            }
        }
    }

    function deleteRow(tableID) {
        try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for (var i = 0; i < rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[1];
                if (chkbox != null && chkbox.checked == true) {
                    if (rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
        } catch (e) {
            alert(e);
        }
    }
</script>
<div class="container">
    <h2>
        Sprinkler Irrigation Component's price <small></small>
    </h2>
    <ul class="responsive-table">
        <li class="table-row">
            <div class="col col-1" data-label="Job Id">
                Email:</div>
            <div class="col col-2" data-label="Customer Name">
                <asp:TextBox CssClass="form-control" ID="c_email" placeholder="Email" runat="server"></asp:TextBox>
            </div>
            <div class="col col-3" data-label="Amount">
            </div>
        </li>
    </ul>
</div>
<div class="container">
    <ul class="responsive-table">
        <li class="table-row">
            <div class="col col-1" data-label="Job Id">
                Nozzle</div>
            <div class="col col-2">
                <table id="dataTable1" class="table-bordered table-hover table-striped table">
                    <tr>
                        <td>
                            <input type="checkbox" name="chk" />
                        </td>
                        <td>
                            <asp:TextBox ID="nozzle_lph" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="nozzle_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col col-3">
            </div>
            <div class="col col-4">
                <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable1')" /><hr />
                <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable1')" />
            </div>
        </li>
    </ul>
</div>
<div class="container">
    <ul class="responsive-table">
        <li class="table-row">
            <div class="col col-1" data-label="Job Id">
                Lateral</div>
            <div class="col col-2">
                <table id="dataTable2" class="table-bordered table-hover table-striped table">
                    <tr>
                        <td>
                            <input type="checkbox" name="chk" />
                        </td>
                        <td>
                            <asp:TextBox ID="txt_lat_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_lat_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col col-3">
            </div>
            <div class="col col-4">
                <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable2')" /><hr />
                <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable2')" />
            </div>
        </li>
    </ul>
</div>
<div class="container">
    <ul class="responsive-table">
        <li class="table-row">
            <div class="col col-1" data-label="Job Id">
                Submain Pipe</div>
            <div class="col col-2">
                <table id="dataTable3" class="table-bordered table-hover table-striped table">
                    <tr>
                        <td>
                            <input type="checkbox" name="chk" />
                        </td>
                        <td>
                            <asp:TextBox ID="txt_smain_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_smain_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col col-3">
            </div>
            <div class="col col-4">
                <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable3')" /><hr />
                <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable3')" />
            </div>
        </li>
    </ul>
</div>
<div class="container">
    <ul class="responsive-table">
        <li class="table-row">
            <div class="col col-1" data-label="Job Id">
                Main Pipe</div>
            <div class="col col-2">
                <table id="dataTable4" class="table-bordered table-hover table-striped table">
                    <tr>
                        <td>
                            <input type="checkbox" name="chk" />
                        </td>
                        <td>
                            <asp:TextBox ID="txt_main_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_main_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col col-3">
            </div>
            <div class="col col-4">
                <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable4')" /><hr />
                <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable4')" />
            </div>
        </li>
    </ul>
</div>
<div class="container">
    <ul class="responsive-table">
        <li class="table-row">
            <div class="col col-1" data-label="Job Id">
                Motor Pumping Unit</div>
            <div class="col col-2">
                <table id="dataTable5" class="table-bordered table-hover table-striped table">
                    <tr>
                        <td>
                            <input type="checkbox" name="chk" />
                        </td>
                        <td>
                            <asp:TextBox ID="txt_mpum_hp" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_mpump_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col col-3">
            </div>
            <div class="col col-4">
                <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable5')" /><hr />
                <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable5')" />
            </div>
        </li>
        <li class="table-row">
            <div class="col col-1" data-label="Job Id">
            </div>
            <div class="col col-2" data-label="Customer Name">
                <asp:Button ID="sprink_submit" runat="server" CssClass="btn btn-danger btn-group-lg active"
                    Text="Submit" OnClick="sprink_submit_Click" />
            </div>
            <div class="col col-3" data-label="Amount">
                <asp:Button ID="Button2" runat="server" CssClass="btn btn-warning btn-group-lg active"
                    Text="Back" /></div>
        </li>
    </ul>
</div>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

protected void sprink_submit_Click(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("INSERT INTO test_sprink(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)", con);
    string[] textboxValues1 = Request.Form.GetValues("c_email");
    string[] textboxValues2 = Request.Form.GetValues("nozzle_lph");
    string[] textboxValues3 = Request.Form.GetValues("nozzle_price");
    string[] textboxValues4 = Request.Form.GetValues("txt_lat_mm");
    string[] textboxValues5 = Request.Form.GetValues("txt_lat_price");
    string[] textboxValues6 = Request.Form.GetValues("txt_smain_mm");
    string[] textboxValues7 = Request.Form.GetValues("txt_smain_price");
    string[] textboxValues8 = Request.Form.GetValues("txt_main_mm");
    string[] textboxValues9 = Request.Form.GetValues("txt_main_price");
    string[] textboxValues10 = Request.Form.GetValues("txt_mpum_hp");
    string[] textboxValues11 = Request.Form.GetValues("txt_mpump_price");
    int length = 0;
    if (textboxValues2.Length > length)
    {
        length = textboxValues2.Length;
    }
    if (textboxValues3.Length > length)
    {
        length = textboxValues3.Length;
    }
    if (textboxValues4.Length > length)
    {
        length = textboxValues4.Length;
    }
    if (textboxValues5.Length > length)
    {
        length = textboxValues5.Length;
    }
    if (textboxValues6.Length > length)
    {
        length = textboxValues6.Length;
    }
    if (textboxValues7.Length > length)
    {
        length = textboxValues7.Length;
    }
    if (textboxValues8.Length > length)
    {
        length = textboxValues8.Length;
    }
    if (textboxValues9.Length > length)
    {
        length = textboxValues9.Length;
    }
    if (textboxValues10.Length > length)
    {
        length = textboxValues10.Length;
    }
    if (textboxValues11.Length > length)
    {
        length = textboxValues11.Length;
    }

    for (int i = 0; i < length; i++)
    {
        cmd = new SqlCommand("INSERT INTO test_sprink(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)", con);
        cmd.Parameters.AddWithValue("@company_email", textboxValues1[0]);
        cmd.Parameters.AddWithValue("@nozzle_lph", textboxValues2.Length > i ? textboxValues2[i] : "");
        cmd.Parameters.AddWithValue("@nozzle_price", textboxValues3.Length > i ? textboxValues3[i] : "");
        cmd.Parameters.AddWithValue("@lateral_mm", textboxValues4.Length > i ? textboxValues4[i] : "");
        cmd.Parameters.AddWithValue("@lateral_price", textboxValues5.Length > i ? textboxValues5[i] : "");
        cmd.Parameters.AddWithValue("@main_mm", textboxValues6.Length > i ? textboxValues6[i] : "");
        cmd.Parameters.AddWithValue("@main_price", textboxValues7.Length > i ? textboxValues7[i] : "");
        cmd.Parameters.AddWithValue("@submain_mm", textboxValues8.Length > i ? textboxValues8[i] : "");
        cmd.Parameters.AddWithValue("@submain_price", textboxValues9.Length > i ? textboxValues9[i] : "");
        cmd.Parameters.AddWithValue("@Sp_mpump_hp", textboxValues10.Length > i ? textboxValues10[i] : "");
        cmd.Parameters.AddWithValue("@Sp_mpump_price", textboxValues11.Length > i ? textboxValues11[i] : "");
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

VB.Net

Protected Sub sprink_submit_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim constr As String = ConfigurationManager.ConnectionStrings("Mycon").ConnectionString
    Dim con As SqlConnection = New SqlConnection(constr)
    Dim cmd As SqlCommand = New SqlCommand("INSERT INTO test_sprink(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)", con)
    Dim textboxValues1 As String() = Request.Form.GetValues("c_email")
    Dim textboxValues2 As String() = Request.Form.GetValues("nozzle_lph")
    Dim textboxValues3 As String() = Request.Form.GetValues("nozzle_price")
    Dim textboxValues4 As String() = Request.Form.GetValues("txt_lat_mm")
    Dim textboxValues5 As String() = Request.Form.GetValues("txt_lat_price")
    Dim textboxValues6 As String() = Request.Form.GetValues("txt_smain_mm")
    Dim textboxValues7 As String() = Request.Form.GetValues("txt_smain_price")
    Dim textboxValues8 As String() = Request.Form.GetValues("txt_main_mm")
    Dim textboxValues9 As String() = Request.Form.GetValues("txt_main_price")
    Dim textboxValues10 As String() = Request.Form.GetValues("txt_mpum_hp")
    Dim textboxValues11 As String() = Request.Form.GetValues("txt_mpump_price")
    Dim length As Integer = 0

    If textboxValues2.Length > length Then
        length = textboxValues2.Length
    End If

    If textboxValues3.Length > length Then
        length = textboxValues3.Length
    End If

    If textboxValues4.Length > length Then
        length = textboxValues4.Length
    End If

    If textboxValues5.Length > length Then
        length = textboxValues5.Length
    End If

    If textboxValues6.Length > length Then
        length = textboxValues6.Length
    End If

    If textboxValues7.Length > length Then
        length = textboxValues7.Length
    End If

    If textboxValues8.Length > length Then
        length = textboxValues8.Length
    End If

    If textboxValues9.Length > length Then
        length = textboxValues9.Length
    End If

    If textboxValues10.Length > length Then
        length = textboxValues10.Length
    End If

    If textboxValues11.Length > length Then
        length = textboxValues11.Length
    End If

    For i As Integer = 0 To length - 1
        cmd = New SqlCommand("INSERT INTO test_sprink(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)", con)
        cmd.Parameters.AddWithValue("@company_email", textboxValues1(0))
        cmd.Parameters.AddWithValue("@nozzle_lph", If(textboxValues2.Length > i, textboxValues2(i), ""))
        cmd.Parameters.AddWithValue("@nozzle_price", If(textboxValues3.Length > i, textboxValues3(i), ""))
        cmd.Parameters.AddWithValue("@lateral_mm", If(textboxValues4.Length > i, textboxValues4(i), ""))
        cmd.Parameters.AddWithValue("@lateral_price", If(textboxValues5.Length > i, textboxValues5(i), ""))
        cmd.Parameters.AddWithValue("@main_mm", If(textboxValues6.Length > i, textboxValues6(i), ""))
        cmd.Parameters.AddWithValue("@main_price", If(textboxValues7.Length > i, textboxValues7(i), ""))
        cmd.Parameters.AddWithValue("@submain_mm", If(textboxValues8.Length > i, textboxValues8(i), ""))
        cmd.Parameters.AddWithValue("@submain_price", If(textboxValues9.Length > i, textboxValues9(i), ""))
        cmd.Parameters.AddWithValue("@Sp_mpump_hp", If(textboxValues10.Length > i, textboxValues10(i), ""))
        cmd.Parameters.AddWithValue("@Sp_mpump_price", If(textboxValues11.Length > i, textboxValues11(i), ""))
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    Next
End Sub