user2210516
user2210516

Reputation: 683

how to insert value to sql db from dynamically generated textboxes asp.net

First of all im really new to Visual Studio and Asp.net C# and so on...

I'm creating a simple rental application for bicycles and just got stuck.

I made a Formula for adding a order for one bike and made an insert into at a submit buttonclick without Problem.

Now to my Problem.. It is possible for the same customer to rent more bikes at the same time and instead of filling out the Formula again for every new bike i want to be able to add more bikes to the same Formula (1 bike = Serial Number)

To do this I found a solution on the web where i took away the Serial Number field and made a field where the user fills in the amount of bikes the customer wants to rent and then a button next to the field. When the user then writes for example 3 in the amount field and presses the button it automatically Pop ups 3 textboxes for Input of every bikes Serial Number.

Now i Need some solution for inserting the value of this textboxes with the Serial numbers..

After pressing the button that makes the insert to my SQL db i have to make some Loop to Count how many inserts i have to make and also print in the different values of the Serial numbers for the each order.

example: 

1 customer 3 bikes

bike 1 = Serial Number 123
bike 2 = Serial Number 234
bike 3 = Serial Number 345 

now i have to make 3 insert into with this 3 different Serial numbers after clicking the send order 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);
        }

    }

Upvotes: 0

Views: 3208

Answers (2)

Ajay
Ajay

Reputation: 2080

Write the below code in btnGenerateControl_Click1 Event(below For Loop)

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

And Write the below code in the btnAddOrder_Click1 event(below For 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();

Upvotes: 1

Ajay
Ajay

Reputation: 2080

Hai I got Output By Using Temperory data(Not like your data).So by Using the Below Logic You can get your output.

At First I take database table like

 orders(id int,person_name varchar(20))

Then according to my database I use the logic below.Here I use label for checking 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 Idea from this Code. Happy Programming......

Upvotes: 0

Related Questions