Hey,
I am using below code to insert dynamically generated textboxvalue into the database. but it is giving me error.please help me to solve it.
<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;
}
}
}
</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>
<asp:GridView ID="gvData" runat="server">
</asp:GridView>
</div>
protected void sprink_submit_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into company_sprinkler_data_test(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);
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@company_email", c_email.Text.Trim().ToString());
//string[] textboxValues1 = Request.Form.GetValues("company_email");
string[] textboxValues2 = Request.Form.GetValues("nozzle_lph");
string[] textboxValues3 = Request.Form.GetValues("nozzle_price");
string[] textboxValues4 = Request.Form.GetValues("lateral_mm");
string[] textboxValues5 = Request.Form.GetValues("lateral_price");
string[] textboxValues6 = Request.Form.GetValues("main_mm");
string[] textboxValues7 = Request.Form.GetValues("main_price");
string[] textboxValues8 = Request.Form.GetValues("submain_mm");
string[] textboxValues9 = Request.Form.GetValues("submain_price");
string[] textboxValues10 = Request.Form.GetValues("Sp_mpump_hp");
string[] textboxValues11 = Request.Form.GetValues("Sp_mpump_price");
for (int i = 0; i < textboxValues2.Length; i++)
{
cmd = new SqlCommand("insert into company_sprinkler_data_test(nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@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[i]);
cmd.Parameters.AddWithValue("@nozzle_lph", textboxValues2[i]);
cmd.Parameters.AddWithValue("@nozzle_price", textboxValues3[i]);
cmd.Parameters.AddWithValue("@lateral_mm", textboxValues4[i]);
cmd.Parameters.AddWithValue("@lateral_price", textboxValues5[i]);
cmd.Parameters.AddWithValue("@main_mm", textboxValues6[i]);
cmd.Parameters.AddWithValue("@main_price", textboxValues7[i]);
cmd.Parameters.AddWithValue("@submain_mm", textboxValues8[i]);
cmd.Parameters.AddWithValue("@submain_price", textboxValues9[i]);
cmd.Parameters.AddWithValue("@Sp_mpump_hp", textboxValues10[i]);
cmd.Parameters.AddWithValue("@Sp_mpump_price", textboxValues11[i]);
}
cmd = new SqlCommand("select company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price from company_sprinkler_data_test", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gvData.DataSource = dt;
gvData.DataBind();
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();
}
}
I am having below error by using the above code
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 47: cmd.Parameters.AddWithValue("@nozzle_lph", textboxValues2[i]);
Line 48: cmd.Parameters.AddWithValue("@nozzle_price", textboxValues3[i]);
Line 49: cmd.Parameters.AddWithValue("@lateral_mm", textboxValues4[i]);
Line 50: cmd.Parameters.AddWithValue("@lateral_price", textboxValues5[i]);
Line 51: cmd.Parameters.AddWithValue("@main_mm", textboxValues6[i]);
Source File: C:\Users\ait_0207\source\repos\ait0207(sem7)\Test_captcha\test_table_dynamic.aspx.cs Line: 49
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Test_captcha.test_table_dynamic.sprink_submit_Click(Object sender, EventArgs e) in C:\Users\ait_0207\source\repos\ait0207(sem7)\Test_captcha\test_table_dynamic.aspx.cs:49
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9796134
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +211
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.3815.0