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
.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)
- {
-
- }
- 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)
- );