TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Nidhi Donga
NA
88
8.9k
Insert data in database from dynamically generated textbox.
Jul 29 2019 11:06 PM
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.
.aspx
<%@ 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>
<style>
body {
font-family:
'lato'
, sans-serif;
}
.container {
max-width: 1000px;
margin-left: auto;
margin-right: auto;
padding-left: 10px;
padding-right: 10px;
}
h2 {
font-size: 26px;
margin: 20px 0;
text-align: center;
}
h2 small {
font-size: 0.5em;
}
.responsive-table li {
border-radius: 3px;
padding: 25px 30px;
display: flex;
justify-content: space-between;
margin-bottom: 25px;
}
.responsive-table .table-header {
background-color: #95A5A6;
font-size: 14px;
text-transform: uppercase;
letter-spacing: 0.03em;
}
.responsive-table .table-row {
background-color: #ffffff;
box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.1);
}
.responsive-table .col-1 {
flex-basis: 5%;
}
.responsive-table .col-2 {
flex-basis: 50%;
}
.responsive-table .col-3 {
flex-basis: 25%;
}
.responsive-table .col-4 {
flex-basis: 25%;
}
@media all and (max-width: 767px) {
.responsive-table .table-header {
display: none;
}
.responsive-table li {
display: block;
}
.responsive-table .col {
flex-basis: 100%;
}
.responsive-table .col {
display: flex;
padding: 10px 0;
}
.responsive-table .col:before {
color: #6C7A89;
padding-right: 10px;
content: attr(data-label);
flex-basis: 50%;
text-align: right;
}
}
</style>
<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>
<body>
<form id=
"form1"
runat=
"server"
>
<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>
</form>
</body>
</html>
.aspx.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.IO;
using
System.Web.Script.Serialization;
namespace
Test_captcha
{
public
partial
class
test_table_dynamic : System.Web.UI.Page
{
SqlConnection con =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"Mycon"
].ToString());
protected
string
Values;
protected
void
Page_Load(
object
sender, EventArgs e)
{
//c_email.Text = Session["Company"] as string;
}
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();
}
}
}
}
Data table
CREATE
TABLE
[dbo].[company_sprinkler_data_test] (
[sprink_id]
INT
IDENTITY (1, 1)
NOT
NULL
,
[company_email]
VARCHAR
(100)
NULL
,
[nozzle_lph]
INT
NULL
,
[noozle_price]
FLOAT
(53)
NULL
,
[lateral_mm]
INT
NULL
,
[lateral_price]
FLOAT
(53)
NULL
,
[main_mm]
INT
NULL
,
[main_price]
FLOAT
(53)
NULL
,
[Sp_mpump_hp]
INT
NULL
,
[Sp_mpump_price]
FLOAT
(53)
NULL
,
PRIMARY
KEY
CLUSTERED ([sprink_id]
ASC
)
);
Reply
Answers (
3
)
Is It Possible to make Datalist as Responsive ?.
Set Session use web api.