Insert dynamically generated TextBox value in database using C# and VB.Net in ASP.Net

Last Reply one year ago By pandeyism

Posted one year ago

I want to insert data in data base from dynamically generated textboxes. I am using below code but it is not working properly and giving me errors.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test_table_dynamic.aspx.cs" Inherits="Test_captcha.test_table_dynamic" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Content/bootstrap.css" rel="stylesheet" />
    <script src="Scripts/jquery-3.0.0.js"></script>
    <script src="Scripts/bootstrap.js"></script>
    <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;
                //alert(newcell.childNodes);
                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>
</head>

 

        protected void sprink_submit_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("insert into company_sprinkler_data_test(company_email,nozzle_lph,noozle_price,lateral_mm,lateral_price,main_mm,main_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@Sp_mpump_hp,@Sp_mpump_price)");
            con.Open();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@company_email", c_email.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@nozzle_lph", nozzle_lph.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@nozzle_price", nozzle_price.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@lateral_mm", txt_lat_mm.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@lateral_price", txt_lat_price.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@submain_mm", txt_smain_mm.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@submain_price", txt_smain_price.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@main_mm", txt_main_mm.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@main_price", txt_main_price.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@Sp_mpump_hp", txt_mpum_hp.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@Sp_mpump_price", txt_mpump_price.Text.Trim().ToString());

            int row;
            try
            {
                row = cmd.ExecuteNonQuery();
                if (row > 0)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Price added Sucessful');window.location='MIS_login.aspx';", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Add Prices again');", true);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();

            }
        }

 

This question does not have replies that have been liked.