Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,
I have written a code to insert the dynamically generated textbox value in database. I want to check all the textbox generated dynamically at a time. If no textbox empty then only it should insert textbox value into database. But in my code it is inserting the textbox value if one of the textbox has serial number value. How to resolve this kindly help.
My code is

C#
if (txtQuantity.Text != "")
       {
           try
           {
               for (int i = 0; i < quantity; i++)
               {

                   if (Request.Form["Textbox"+i.ToString()].Trim()== "")
                   {

                     Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Enter Serial Number');",  true);

                     //Button2.Attributes.Add("onClick", "return false;");
                   }

                   else

                   {
                      // Label17.Visible = false;
                       con.Open();
                       string qryInsertProduct = "insert into Product_Details(GatePassNo,ClientID,Product_name,SerialNo,Status,CheckIN_Date ,Customer_Name, Customer_location, System_Date, [GatePass Status],State, City,S) values('" + GatePassNo + "','" + clientID + "','" + productName + "','" + Request.Form["Textbox" + i.ToString()] + "','" + status + "','" + TextBox1.Text + "','" + ddlCName.SelectedItem.Value + "','" + ddlCLocation.SelectedItem.Value + "','" + Label11.Text + "', 'Open','" + DropDownList1.SelectedItem.Value + "','" + DropDownList2.SelectedItem.Value + "','0')";
                       SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct, con);
                       comInsertProduct.ExecuteNonQuery();
                       con.Close();
                       gdvProducts.Visible = true;
                       PopulateGridView();
                   }

               }
               }


           catch (Exception ex)
           {

           }

           finally
           {
               if (con.State == ConnectionState.Open)
               {
                   con.Close();

               }
           }
       }
Posted
Updated 22-Jul-14 22:53pm
v2
Comments
nilesh sawardekar 23-Jul-14 4:58am    
please explain ,issue your facing

1 solution

You have bigger problems than you think...

Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Particularly in a web based application, where I could delete your whole database from the other side of the world!

But...to the problem you have noticed: Simple: You only check one textbox. If it's not empty you insert. Which means that if you have four textboxes, you insert up to four times...

Check them first, in one loop, and build up a list of items to insert.
If they all pass, then do one single insert operation. If any fail, report the error and exit.
 
Share this answer
 
Comments
nilesh sawardekar 23-Jul-14 5:09am    
also
"Textbox" + i.text.ToString()
OriginalGriff 23-Jul-14 5:16am    
Sorry? That generates the string name of his "dynamic" textboxes:
Testbox0
Testbox1
Testbox2
Testbox3
...
Which Request.Form uses as an index to find the actual control content.
nilesh sawardekar 23-Jul-14 5:23am    
He needed text in textbox so..
Textbox" + i.text
OriginalGriff 23-Jul-14 5:35am    
Not so: Request.Form takes the name of the control as an index, and returns the text value of that control as a string. And a string doesn't have a Text property!
Neither does an integer as your code shows... :laugh:

Try it: you'll see what I mean.
Member 10578683 23-Jul-14 6:08am    
Hello. how to do it. i tried to do but getting error. Kindly give some code

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