[Solved] Dynamic TextBox value inserted zero in database using C# in ASP.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

Hey, In below code whatever value i am inserting in txtbox it only stores zero(0) in data base.so please help me..aspx.cs

namespace Test_captcha
{
    public partial class test_table_dynamic : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            //c_email.Text = Session["Company"] as string;
        }

        protected void sprink_submit_Click(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("INSERT INTO test_sprink(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);
            string[] textboxValues1 = Request.Form.GetValues("c_email");
            string[] textboxValues2 = Request.Form.GetValues("nozzle_lph");
            string[] textboxValues3 = Request.Form.GetValues("nozzle_price");
            string[] textboxValues4 = Request.Form.GetValues("txt_lat_mm");
            string[] textboxValues5 = Request.Form.GetValues("txt_lat_price");
            string[] textboxValues6 = Request.Form.GetValues("txt_smain_mm");
            string[] textboxValues7 = Request.Form.GetValues("txt_smain_price");
            string[] textboxValues8 = Request.Form.GetValues("txt_main_mm");
            string[] textboxValues9 = Request.Form.GetValues("txt_main_price");
            string[] textboxValues10 = Request.Form.GetValues("txt_mpum_hp");
            string[] textboxValues11 = Request.Form.GetValues("txt_mpump_price");


            int length = 0;
            if (textboxValues2.Length > length)
            {
                length = textboxValues2.Length;
            }
            else if (textboxValues3.Length > length)
            {
                length = textboxValues3.Length;
            }
            else if (textboxValues4.Length > length)
            {
                length = textboxValues4.Length;
            }
            else if (textboxValues5.Length > length)
            {
                length = textboxValues5.Length;
            }
            else if (textboxValues6.Length > length)
            {
                length = textboxValues6.Length;
            }
            else if (textboxValues7.Length > length)
            {
                length = textboxValues7.Length;
            }
            else if (textboxValues8.Length > length)
            {
                length = textboxValues8.Length;
            }
            else if (textboxValues9.Length > length)
            {
                length = textboxValues9.Length;
            }
            else if (textboxValues10.Length > length)
            {
                length = textboxValues10.Length;
            }
            else if (textboxValues11.Length > length)
            {
                length = textboxValues11.Length;
            }

            for (int i = 0; i < length; i++)
            {
                cmd = new SqlCommand("INSERT INTO test_sprink(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);
                cmd.Parameters.AddWithValue("@company_email", textboxValues1[0]);
                cmd.Parameters.AddWithValue("@nozzle_lph", textboxValues2[i].Length < i ? textboxValues2[i] : "");
                cmd.Parameters.AddWithValue("@nozzle_price", textboxValues3[i].Length < i ? textboxValues3[i] : "");
                cmd.Parameters.AddWithValue("@lateral_mm", textboxValues4[i].Length < i ? textboxValues4[i] : "");
                cmd.Parameters.AddWithValue("@lateral_price", textboxValues5[i].Length < i ? textboxValues5[i] : "");
                cmd.Parameters.AddWithValue("@main_mm", textboxValues6[i].Length < i ? textboxValues6[i] : "");
                cmd.Parameters.AddWithValue("@main_price", textboxValues7[i].Length < i ? textboxValues7[i] : "");
                cmd.Parameters.AddWithValue("@submain_mm", textboxValues8[i].Length < i ? textboxValues8[i] : "");
                cmd.Parameters.AddWithValue("@submain_price", textboxValues9[i].Length < i ? textboxValues9[i] : "");
                cmd.Parameters.AddWithValue("@Sp_mpump_hp", textboxValues10[i].Length < i ? textboxValues10[i] : "");
                cmd.Parameters.AddWithValue("@Sp_mpump_price", textboxValues11[i].Length < i ? textboxValues11[i] : "");
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}

datatable

CREATE TABLE [dbo].[test_sprink] (
    [sprink_id]      INT           IDENTITY (1, 1) NOT NULL,
    [company_email]  VARCHAR (100) NOT NULL,
    [nozzle_lph]     INT           NOT NULL,
    [nozzle_price]   FLOAT (53)    NOT NULL,
    [lateral_mm]     INT           NOT NULL,
    [lateral_price]  FLOAT (53)    NOT NULL,
    [main_mm]        INT           NOT NULL,
    [main_price]     FLOAT (53)    NOT NULL,
    [submain_mm]     INT           NOT NULL,
    [submain_price]  FLOAT (53)    NOT NULL,
    [Sp_mpump_hp]    INT           NOT NULL,
    [Sp_mpump_price] FLOAT (53)    NOT NULL,
    PRIMARY KEY CLUSTERED ([sprink_id] ASC)
);

 

This question does not have replies that have been liked.