Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (txtQuantity.Text != "")
        {
            try
            {
                for (int i = 0; i < quantity; i++)
                {
                    foreach (Control ctrl in this.Controls)
                    {
                        TextBox txtBox1 = new TextBox();
                        txtBox1.ID = "tb1" + i.ToString();
                        txtBox1.Text = txtBox1.Text;
                        PlaceHolder1.Controls.Add(txtBox1);
                        if (ctrl is TextBox)
                        {
                            
                            con.Open();  
                            string qryInsertProduct = "insert into Product_Details(GatePassNo,ClientID,Product_name,SerialNo,Status,CheckIN_Date ,Customer_Name, Customer_location) values('" + GatePassNo + "','" + clientID + "','" + productName + "','" + txtBox1.Text + "','" + status + "','" + TextBox1.Text + "','" + ddlCName.SelectedItem.Value + "','" + ddlCLocation.SelectedItem.Value + "')";
                            SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct, con);
                            comInsertProduct.ExecuteNonQuery();
                        }
                    }


This my code, please help.
Posted
Updated 25-Feb-14 21:55pm
v2
Comments
Maciej Los 26-Feb-14 3:55am    
And the issue is...
Member 10578683 26-Feb-14 4:09am    
according to quantity the textboxes are dynamically generating. i need the value that i m writing in that dynamically generated textboxes into my database. SerialNo is the column where i want to insert. How will i do this. please help.
V5709 26-Feb-14 5:59am    
what is the error in this code? you said that you want to insert text value of dynamically generated text boxes into db. But here creation of text boxes & insertion of its values into db are in same function .How it is possible?how you are going to write text in dynamic textbox?
Member 10578683 26-Feb-14 6:13am    
i have written the code to create the textboxes in othher btn Click event.here i wrote the code to insert
V5709 26-Feb-14 6:14am    
then where thing is going wrong?

As per i understand you're trying to dynamically create TextBox control, assign a value to it and save its value into database. You did not provide enough information to provide direct answer.

Looking at code you've provided, i do not understand why do you need to create TextBox dynamically, especially when you create TextBox in the same code which is used to save value into database. It means that user can't enter/type any value into newly created textbox.

Sorry, but it looks like code-dump. You'll not be able go further, until you do not understand what your code does.

I would suggest to start here:
ASP.NET[^]
Connecting to Databases in ASP.NET[^]
Walkthrough: Editing and Inserting Data in Web Pages with the DetailsView Web Server Control[^]

On the other hand, you should read about SQL Injection[^].
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]
 
Share this answer
 
Just look at your code.

C#
for (int i = 0; i < quantity; i++)
                {
                    foreach (Control ctrl in this.Controls)
                    {
                        TextBox txtBox1 = new TextBox();
                        txtBox1.ID = "tb1" + i.ToString();
                        txtBox1.Text = txtBox1.Text;
                        PlaceHolder1.Controls.Add(txtBox1);
                        if (ctrl is TextBox)
                        {
                            
                            con.Open();  
                            string qryInsertProduct = "insert into Product_Details(GatePassNo,ClientID,Product_name,SerialNo,Status,CheckIN_Date ,Customer_Name, Customer_location) values('" + GatePassNo + "','" + clientID + "','" + productName + "','" + txtBox1.Text + "','" + status + "','" + TextBox1.Text + "','" + ddlCName.SelectedItem.Value + "','" + ddlCLocation.SelectedItem.Value + "')";
                            SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct, con);
                            comInsertProduct.ExecuteNonQuery();
                        }
                    }


Here in loop you have created text box controls.Just followed by an insert statement.
You are assigning text to dynamically created text boxes by the code line
C#
txtBox1.Text = txtBox1.Text;


Here both 'txtBox1.Text' are blank.That's why you are not getting any text value to insert.
 
Share this answer
 
v2
Comments
Member 10578683 27-Feb-14 0:47am    
ya i understood what was the problem. thanks
V5709 27-Feb-14 0:53am    
Please mark solution as answer...

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