How to insert value in sql db from asp.net dynamically generated text fields

First of all, I'm really new to Visual Studio and Asp.net C #, etc.

I am creating a simple bike rental app and just stuck.

I made a Formula to add order for one bike and made an insert into the submit button without any problems.

Now to my problem .. The same client can rent more bikes at the same time, and instead of filling out the Formula for each new bike again, I want to add more bikes to the same Formula (1 bike = serial number)

To do this, I found a solution on the Internet, where I took the Serial Number field and created a field in which the user fills in the number of bikes that the client wants to rent, and then a button next to the field. When the user then writes, for example, 3 in the quantity field and presses the button, he automatically pops up 3 text fields for entering each bike Serial number.

Now I need some solution to insert the value of these text fields with serial numbers.

After clicking the button that makes the insert on my SQL server, I have to do some loop to count the number of attachments that I need to make, and also print in different values โ€‹โ€‹of serial numbers for each order.

example: 1 customer 3 bikes bike 1 = Serial Number 123 bike 2 = Serial Number 234 bike 3 = Serial Number 345 

Now I need to make 3 inserts with these 3 different serial numbers after clicking the submit button.

Am I on the right track?

 protected void btnGenerateControl_Click(object sender, EventArgs e) { int Count = Convert.ToInt32(Qty.Text); for(int i =1; i <= Count; i++) { TextBox txtbox = new TextBox(); txtbox.Text = "Textbox - " + i.ToString(); pnlTextBoxes.Controls.Add(txtbox); } } protected void btnAddOrder_Click(object sender, EventArgs e) { int Count = Convert.ToInt32(Qty.Text); for (int i = 1; i <= Count; i++) { String query = "insert into Orders (CustID, OrderDate, Time, ProductID, ProjectID, Status, FlottenID)values('" + CustID.Text + "','" + OrderDate.Text + "','" + Time.Text + "','" + ProductID.Value + "','" + ProjectID.Value + "','" + Status.Value +"','" +HERE I NEED TO CATCH THE VALUE OF SERIAL NUMBER+ "')"; String query1 = "commit;"; DataLayer.DataConnector dat = new DataLayer.DataConnector("Provider=SQLOLEDB; data source=****;database=event;user ID=****;password=*****; Persist Security Info=False"); dat.DataInsert(query); dat.DataInsert(query1); } } 
0
source share
2 answers

Write the code below in the btnGenerateControl_Click1 event (below for the loop)

  pnlTextBoxes.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "'type='text' />")); pnlTextBoxes.Controls.Add(new LiteralControl("<br />")); 

And write the code below in the btnAddOrder_Click1 event (below for the loop)

 String query = "insert into Orders (CustID, OrderDate, Time, ProductID, ProjectID, Status, FlottenID)values('" + CustID.Text + "','" + OrderDate.Text + "','" + Time.Text + "','" + ProductID.Value + "','" + ProjectID.Value + "','" + Status.Value + "','" + Request.Form["Textbox" + i.ToString()] + "')"; MySqlCommand cmd = new MySqlCommand(query,con); cmd.ExecuteNonQuery(); 
+1
source

I got the result using Temperature data (not like your data). So using the logic below. You can get your result.

First I take a database table, for example

  orders(id int,person_name varchar(20)) 

Then, according to my database, I use the following logic. I use the label to check only

 public partial class stack_over : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } string connString = "Your Database Connection String"; protected void btnGenerateControl_Click1(object sender, EventArgs e) { int Count = Convert.ToInt32(Qty.Text); for (int i = 1; i <= Count; i++) { pnlTextBoxes.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "' type='text' />")); pnlTextBoxes.Controls.Add(new LiteralControl("<br />")); } } protected void btnAddOrder_Click1(object sender, EventArgs e) { MySqlConnection con = new MySqlConnection(connString); con.Open(); int Count = Convert.ToInt32(Qty.Text); for (int i = 1; i <= Count; i++) { Label1.Text = Request.Form["Textbox"+i.ToString()]; String query = "insert into Orders values('" + Label1.Text + "','ajay')"; MySqlCommand cmd = new MySqlCommand(query,con); cmd.ExecuteNonQuery(); } } 

}

Hope you can get the Idea from this Code. Happy programming ......

0
source

Source: https://habr.com/ru/post/1496221/


All Articles