Nidhi Donga

Nidhi Donga

  • NA
  • 88
  • 8.8k

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
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test_table_dynamic.aspx.cs" Inherits="Test_captcha.test_table_dynamic" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. <link href="Content/bootstrap.css" rel="stylesheet" />  
  7. <script src="Scripts/jquery-3.0.0.js"></script>  
  8. <script src="Scripts/bootstrap.js"></script>  
  9. <style>  
  10. body {  
  11. font-family: 'lato', sans-serif;  
  12. }  
  13. .container {  
  14. max-width: 1000px;  
  15. margin-left: auto;  
  16. margin-right: auto;  
  17. padding-left: 10px;  
  18. padding-right: 10px;  
  19. }  
  20. h2 {  
  21. font-size: 26px;  
  22. margin: 20px 0;  
  23. text-align: center;  
  24. }  
  25. h2 small {  
  26. font-size: 0.5em;  
  27. }  
  28. .responsive-table li {  
  29. border-radius: 3px;  
  30. padding: 25px 30px;  
  31. display: flex;  
  32. justify-content: space-between;  
  33. margin-bottom: 25px;  
  34. }  
  35. .responsive-table .table-header {  
  36. background-color: #95A5A6;  
  37. font-size: 14px;  
  38. text-transform: uppercase;  
  39. letter-spacing: 0.03em;  
  40. }  
  41. .responsive-table .table-row {  
  42. background-color: #ffffff;  
  43. box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.1);  
  44. }  
  45. .responsive-table .col-1 {  
  46. flex-basis: 5%;  
  47. }  
  48. .responsive-table .col-2 {  
  49. flex-basis: 50%;  
  50. }  
  51. .responsive-table .col-3 {  
  52. flex-basis: 25%;  
  53. }  
  54. .responsive-table .col-4 {  
  55. flex-basis: 25%;  
  56. }  
  57. @media all and (max-width: 767px) {  
  58. .responsive-table .table-header {  
  59. display: none;  
  60. }  
  61. .responsive-table li {  
  62. display: block;  
  63. }  
  64. .responsive-table .col {  
  65. flex-basis: 100%;  
  66. }  
  67. .responsive-table .col {  
  68. display: flex;  
  69. padding: 10px 0;  
  70. }  
  71. .responsive-table .col:before {  
  72. color: #6C7A89;  
  73. padding-right: 10px;  
  74. content: attr(data-label);  
  75. flex-basis: 50%;  
  76. text-align: right;  
  77. }  
  78. }  
  79. </style>  
  80. <script type="text/javascript">  
  81. function addRow(tableID) {  
  82. var table = document.getElementById(tableID);  
  83. var rowCount = table.rows.length;  
  84. var row = table.insertRow(rowCount);  
  85. var colCount = table.rows[0].cells.length;  
  86. for (var i = 0; i < colCount; i++) {  
  87. var newcell = row.insertCell(i);  
  88. newcell.innerHTML = table.rows[0].cells[i].innerHTML;  
  89. //alert(newcell.childNodes);  
  90. switch (newcell.childNodes[0].type) {  
  91. case "text":  
  92. newcell.childNodes[0].value = "";  
  93. break;  
  94. case "checkbox":  
  95. newcell.childNodes[0].checked = false;  
  96. break;  
  97. case "select-one":  
  98. newcell.childNodes[0].selectedIndex = 0;  
  99. break;  
  100. }  
  101. }  
  102. }  
  103. function deleteRow(tableID) {  
  104. try {  
  105. var table = document.getElementById(tableID);  
  106. var rowCount = table.rows.length;  
  107. for (var i = 0; i < rowCount; i++) {  
  108. var row = table.rows[i];  
  109. var chkbox = row.cells[0].childNodes[1];  
  110. if (chkbox != null && chkbox.checked == true) {  
  111. if (rowCount <= 1) {  
  112. alert("Cannot delete all the rows.");  
  113. break;  
  114. }  
  115. table.deleteRow(i);  
  116. rowCount--;  
  117. i--;  
  118. }  
  119. }  
  120. catch (e) {  
  121. alert(e);  
  122. }  
  123. }  
  124. </script>  
  125. </head>  
  126. <body>  
  127. <form id="form1" runat="server">  
  128. <div class="container">  
  129. <h2>Sprinkler Irrigation Component's price <small></small></h2>  
  130. <ul class="responsive-table">  
  131. <li class="table-row">  
  132. <div class="col col-1" data-label="Job Id">Email:</div>  
  133. <div class="col col-2" data-label="Customer Name">  
  134. <asp:TextBox CssClass="form-control" ID="c_email" placeholder="Email" runat="server"></asp:TextBox>  
  135. </div>  
  136. <div class="col col-3" data-label="Amount"></div>  
  137. </li>  
  138. </ul>  
  139. </div>  
  140. <div class="container">  
  141. <ul class="responsive-table">  
  142. <li class="table-row">  
  143. <div class="col col-1" data-label="Job Id">Nozzle</div>  
  144. <div class="col col-2">  
  145. <table id="dataTable1" class="table-bordered table-hover table-striped table">  
  146. <tr>  
  147. <td>  
  148. <input type="checkbox" name="chk" /></td>  
  149. <td>  
  150. <asp:TextBox ID="nozzle_lph" runat="server" type="text" placeholder="LPH"></asp:TextBox></td>  
  151. <td>  
  152. <asp:TextBox ID="nozzle_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox></td>  
  153. </tr>  
  154. </table>  
  155. </div>  
  156. <div class="col col-3"></div>  
  157. <div class="col col-4">  
  158. <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable1')" /><hr />  
  159. <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable1')" />  
  160. </div>  
  161. </li>  
  162. </ul>  
  163. </div>  
  164. <div class="container">  
  165. <ul class="responsive-table">  
  166. <li class="table-row">  
  167. <div class="col col-1" data-label="Job Id">Lateral</div>  
  168. <div class="col col-2">  
  169. <table id="dataTable2" class="table-bordered table-hover table-striped table">  
  170. <tr>  
  171. <td>  
  172. <input type="checkbox" name="chk" /></td>  
  173. <td>  
  174. <asp:TextBox ID="txt_lat_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox></td>  
  175. <td>  
  176. <asp:TextBox ID="txt_lat_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox></td>  
  177. </tr>  
  178. </table>  
  179. </div>  
  180. <div class="col col-3"></div>  
  181. <div class="col col-4">  
  182. <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable2')" /><hr />  
  183. <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable2')" />  
  184. </div>  
  185. </li>  
  186. </ul>  
  187. </div>  
  188. <div class="container">  
  189. <ul class="responsive-table">  
  190. <li class="table-row">  
  191. <div class="col col-1" data-label="Job Id">Submain Pipe</div>  
  192. <div class="col col-2">  
  193. <table id="dataTable3" class="table-bordered table-hover table-striped table">  
  194. <tr>  
  195. <td>  
  196. <input type="checkbox" name="chk" /></td>  
  197. <td>  
  198. <asp:TextBox ID="txt_smain_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox></td>  
  199. <td>  
  200. <asp:TextBox ID="txt_smain_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox></td>  
  201. </tr>  
  202. </table>  
  203. </div>  
  204. <div class="col col-3"></div>  
  205. <div class="col col-4">  
  206. <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable3')" /><hr />  
  207. <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable3')" />  
  208. </div>  
  209. </li>  
  210. </ul>  
  211. </div>  
  212. <div class="container">  
  213. <ul class="responsive-table">  
  214. <li class="table-row">  
  215. <div class="col col-1" data-label="Job Id">Main Pipe</div>  
  216. <div class="col col-2">  
  217. <table id="dataTable4" class="table-bordered table-hover table-striped table">  
  218. <tr>  
  219. <td>  
  220. <input type="checkbox" name="chk" /></td>  
  221. <td>  
  222. <asp:TextBox ID="txt_main_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox></td>  
  223. <td>  
  224. <asp:TextBox ID="txt_main_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox></td>  
  225. </tr>  
  226. </table>  
  227. </div>  
  228. <div class="col col-3"></div>  
  229. <div class="col col-4">  
  230. <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable4')" /><hr />  
  231. <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable4')" />  
  232. </div>  
  233. </li>  
  234. </ul>  
  235. </div>  
  236. <div class="container">  
  237. <ul class="responsive-table">  
  238. <li class="table-row">  
  239. <div class="col col-1" data-label="Job Id">Motor Pumping Unit</div>  
  240. <div class="col col-2">  
  241. <table id="dataTable5" class="table-bordered table-hover table-striped table">  
  242. <tr>  
  243. <td>  
  244. <input type="checkbox" name="chk" /></td>  
  245. <td>  
  246. <asp:TextBox ID="txt_mpum_hp" runat="server" type="text" placeholder="LPH"></asp:TextBox></td>  
  247. <td>  
  248. <asp:TextBox ID="txt_mpump_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox></td>  
  249. </tr>  
  250. </table>  
  251. </div>  
  252. <div class="col col-3"></div>  
  253. <div class="col col-4">  
  254. <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable5')" /><hr />  
  255. <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable5')" />  
  256. </div>  
  257. </li>  
  258. <li class="table-row">  
  259. <div class="col col-1" data-label="Job Id"></div>  
  260. <div class="col col-2" data-label="Customer Name">  
  261. <asp:Button ID="sprink_submit" runat="server" CssClass="btn btn-danger btn-group-lg active" Text="Submit" OnClick="sprink_submit_Click" />  
  262. </div>  
  263. <div class="col col-3" data-label="Amount">  
  264. <asp:Button ID="Button2" runat="server" CssClass="btn btn-warning btn-group-lg active" Text="Back" /></div>  
  265. </li>  
  266. </ul>  
  267. </div>  
  268. </form>  
  269. </body>  
  270. </html>  
.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Configuration;  
  10. using System.IO;  
  11. using System.Web.Script.Serialization;  
  12. namespace Test_captcha  
  13. {  
  14. public partial class test_table_dynamic : System.Web.UI.Page  
  15. {  
  16. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ToString());  
  17. protected string Values;  
  18. protected void Page_Load(object sender, EventArgs e)  
  19. {  
  20. //c_email.Text = Session["Company"] as string;  
  21. }  
  22. protected void sprink_submit_Click(object sender, EventArgs e)  
  23. {  
  24. 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)");  
  25. con.Open();  
  26. cmd.Connection = con;  
  27. cmd.CommandType = CommandType.Text;  
  28. cmd.Parameters.AddWithValue("@company_email", c_email.Text.Trim().ToString());  
  29. cmd.Parameters.AddWithValue("@nozzle_lph", nozzle_lph.Text.Trim().ToString());  
  30. cmd.Parameters.AddWithValue("@nozzle_price", nozzle_price.Text.Trim().ToString());  
  31. cmd.Parameters.AddWithValue("@lateral_mm", txt_lat_mm.Text.Trim().ToString());  
  32. cmd.Parameters.AddWithValue("@lateral_price", txt_lat_price.Text.Trim().ToString());  
  33. cmd.Parameters.AddWithValue("@submain_mm", txt_smain_mm.Text.Trim().ToString());  
  34. cmd.Parameters.AddWithValue("@submain_price", txt_smain_price.Text.Trim().ToString());  
  35. cmd.Parameters.AddWithValue("@main_mm", txt_main_mm.Text.Trim().ToString());  
  36. cmd.Parameters.AddWithValue("@main_price", txt_main_price.Text.Trim().ToString());  
  37. cmd.Parameters.AddWithValue("@Sp_mpump_hp", txt_mpum_hp.Text.Trim().ToString());  
  38. cmd.Parameters.AddWithValue("@Sp_mpump_price", txt_mpump_price.Text.Trim().ToString());  
  39. int row;  
  40. try  
  41. {  
  42. row = cmd.ExecuteNonQuery();  
  43. if (row > 0)  
  44. {  
  45. ScriptManager.RegisterStartupScript(this, GetType(), "alert""alert('Price added Sucessful');window.location='MIS_login.aspx';"true);  
  46. }  
  47. else  
  48. {  
  49. ScriptManager.RegisterStartupScript(this, GetType(), "alert""alert('Add Prices again');"true);  
  50. }  
  51. }  
  52. catch (Exception)  
  53. {  
  54. throw;  
  55. }  
  56. finally  
  57. {  
  58. if (con.State == ConnectionState.Open)  
  59. con.Close();  
  60. }  
  61. }  
  62. }  
  63. }  
Data table
  1. CREATE TABLE [dbo].[company_sprinkler_data_test] (  
  2. [sprink_id] INT IDENTITY (1, 1) NOT NULL,  
  3. [company_email] VARCHAR (100) NULL,  
  4. [nozzle_lph] INT NULL,  
  5. [noozle_price] FLOAT (53) NULL,  
  6. [lateral_mm] INT NULL,  
  7. [lateral_price] FLOAT (53) NULL,  
  8. [main_mm] INT NULL,  
  9. [main_price] FLOAT (53) NULL,  
  10. [Sp_mpump_hp] INT NULL,  
  11. [Sp_mpump_price] FLOAT (53) NULL,  
  12. PRIMARY KEY CLUSTERED ([sprink_id] ASC)  
  13. );  

Answers (3)