Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hi i am creating dynamic text boxes by the code
C#
for (int i = 1; i <= a; i++)
            {
                int c = b + i;
                Label lbl = new Label();
                lbl.ID = Convert.ToString(b + i);
                lbl.Text = "flat no."+ c;
                lbl.Width = 70;

                

                Label lbl1 = new Label();
                lbl1.ID = "lbl1" + i+100;
                lbl1.Text = "Size(Sq.ft)";
                lbl1.Width = 70;

                pnlInfo.Controls.Add(new LiteralControl("   "));

                TextBox txt = new TextBox();
                txt.ID = "txt" + i;
                txt.Width = 70;


                pnlInfo.Controls.Add(new LiteralControl("   "));

                Label lbl2 = new Label();
                lbl2.ID = "lbl2" + i+1000;
                lbl2.Text = "type";
                lbl2.Width = 70;

                pnlInfo.Controls.Add(new LiteralControl("   "));

                TextBox txt1 = new TextBox();
                txt1.ID = "text1" + i;
                txt1.Width = 70;

                pnlInfo.Controls.Add(lbl);
                pnlInfo.Controls.Add(new LiteralControl("  "));
                pnlInfo.Controls.Add(lbl1);
                pnlInfo.Controls.Add(new LiteralControl("  "));
                pnlInfo.Controls.Add(txt);
                pnlInfo.Controls.Add(new LiteralControl("  "));
                pnlInfo.Controls.Add(lbl2);
                pnlInfo.Controls.Add(new LiteralControl("  "));
                pnlInfo.Controls.Add(txt1);

                pnlInfo.Controls.Add(new LiteralControl("<br />"));


                Button2.Visible = true;
}

and created successfully
now i want to update records in my database from these text boxes
by the code
C#
protected void Button2_Click(object sender, EventArgs e)
        {
            int b=Convert.ToInt32(DropDownList3.SelectedItem.Text);
            b=b*100;
            int a = Convert.ToInt32(Label2.Text);
            for (int i = 1; i <= a; i++)
            {
                TextBox txtsize = (TextBox)pnlInfo.FindControl((Convert.ToString("txt" + i)));
                TextBox txttype = (TextBox)pnlInfo.FindControl(Convert.ToString("text1" + i));
                //Label fltno = (Label)pnlInfo.FindControl(Convert.ToString(b + i));

                SqlCommand cmd = new SqlCommand("update flatconfig set size='" + txtsize.Text + "',type='" + txttype.Text + "' where town='" + DropDownList1.SelectedItem.Text + "' and tower='" + DropDownList4.SelectedItem.Text + "' and floor='" + DropDownList3.SelectedItem.Text + "' and flatno='" + i + "'", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }


but its give an error is this code is incorrec
Posted
Updated 29-Aug-12 22:49pm
v2
Comments
Christian Amado 30-Aug-12 9:22am    
In what line do you have the error?
[no name] 30-Aug-12 15:40pm    
First thing is that you should be using parameterized queries. Never use string concatenation to build SQL queries. Your problem is probably in and flatno='" + i + "'" assuming flatno is supposed to be a number and not a string. Should be and flatno=" + i.ToString(). But that is just a guess as you did not say what the error was that you are getting.
sahabiswarup 1-Sep-12 16:23pm    
have you tried breakpoint? which line throw error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900