简体   繁体   中英

inserting dynamic data into a sql server in asp.net with C#

This is possibly a formatting problem. How do I insert Dynamic Data into a SQL server?

The inserts:

 //Orders
int howMan = DropDownList1.SelectedIndex;
int i = 0;
while (i < howMan)
{
    string insertNewOrder = "InSERT INTO tblOrders(orderName, orderQuabt, orderPricePer, orderTotal, orderInput, orderCustName)";
    insertNewOrder += "VALUES(@ordernumber, @orderQuant, @orderPrice,@orderTotal, @orderCust, )";

    SqlCommand sql0rder = new SqlCommand(insertNewOrder, hookup);
    sql0rder.Parameters.Add("@ordernumber", Item.Text); 
    sql0rder.Parameters.Add("@orderQuant", Item.Text);
    sql0rder.Parameters.Add("@orderPrice", Item.Text);
    sql0rder.Parameters.Add("@orderTotal", Item.Text);
    sql0rder.Parameters.Add("@orderInput", Item.Text);
    sql0rder.Parameters.Add("@orderCust", Item.Text);
    hookup.Open();
    sql0rder.ExecuteNonQuery();
    hookup.Close();
    i++;
}

Here are the Dynamic textboxes:

    int howMan = DropDownList1.SelectedIndex;
    int i;
    Control c;

    for (i = 0; i < howMan; i++ )
    {
        c = this.ParseControl("<table><tr><th>OrderNumber</th><td><asp:TextBox id='orderNumber" + i + "' runat='server' CssClass='input'/></td></tr><tr><th>What did they order?</th><td><asp:TextBox id='product" + i + "' runat='server' CssClass='input'/> </td> </tr>   <tr><th>Quantity</th><td><asp:TextBox id='Quant" + i + "' runat='server' CssClass='input'/> </td></tr> <tr><th>Price</th><td><asp:TextBox id='price" + i + "' runat='server' CssClass='input'/></td>  </tr><tr><th>Total Cost Javascript</th><td><asp:Label ID='cost"+i+"' runat='server' Text=''></asp:Label></td> </tr> </table><br><br>");
        TextBoxesHere.Controls.Add(c);  
    }

In the inserts I was trying something of the like Item+i.Text or Item[i].Text. My mindset was what you see with PHP or Javascript. So how do I do this?

Edit: I've had to move on from this issue, and use a way that doesn't involve loops. However the main thing I changed was the dynamic textboxes. Instead of the being created I decided to just use visibility.

are you sure you don't want to use the Entity Framework?

Add -> new element -> Data -> ADO.NET Entity Data Model

open the edmx, right click, refresh from database

I'm not a DB expert, but EntityFramework seems better than the traditional SqlCommand/SqlAdaptor way to me (feel free to correct me/show me the best way if I'm wrong)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM