Skip to content
Advertisement

Looping an SQL statement on razor

Attempted to create a ‘cart’ which holds selected items by the user but upon form completion, all the data from the ‘cart’ is inserted into one row

Example:

OrderID     StockID
Or002       IT01,IT02,IT03

How do I separate the data into a row each?

Intended Result:

OrderID     StockID
Or002       IT01
Or002       IT02
Or002       IT03

How it works:

  1. The user will click a button that will generate the following HTML codes.

(Javascript)

var cartRowContents = `
    <a class="dec" id="dec">-</a>
    <input class="quan-num" id="number" name="FQty" value="1"/>
    <a class="inc" id="inc">+</a>
    <input type="hidden" id="FID" name="FID" value=${id} />
    <div class  = "rem">REMOVE ITEM</div>`;

The input named FID is carrying a passed down value of the ID of the item selected by the user; The generated HTML code will be inserted into the form/cart. The user can’t add the same item but can modify the quantity (value of <input name="FQty"> ) using the <a id="dec"> and <a id="inc">.

  1. When the user completes their order, the following codes will be running

(C#)

@functions{

public static String insD(Database db, String query) {

    IEnumerable<dynamic>  data;

    data = db.Query(query); //Executing the Query

       return query;

    }

}

(ASP.NET Web Pages)

@{

 Database db = Database.Open("geh");

 String m_OrderID = "Or002";

 if (IsPost) {  //if statement when form is submitted

        string FID = Request.Form["FID"];
        var Qty = Request.Form["FQty"];

        String query = "INSERT INTO gah.order VALUES ('" + m_OrderID + "','" + FID + "','" + Qty + "')";

        insD(db, query);

}

What I’ve tried:

(ASP.NET Web Pages)

@{

if (IsPost) {
      Database db = Database.Open("geh");
      String m_OrderID = "Or002";

           for(int i = 0; i < 2 ; i++){

                var FID = Request.Form["FID"][i];

                var Qty = Request.Form["FQty"][i];

                String query = "INSERT INTO gah.orders VALUES ('" + m_OrderID + "','" + FID + "', '" + Qty + "')";

                insD(db, query);
       }
   }
}

Results in inserting the first character of the ID.

Example:

ID = "DE002"

OrderID      StockID

Or002        D

Database Info:

OrderID(PK) varchar(10),
StockID(PK) varchar(10),
Qty int()

Advertisement

Answer

If I understand correctly, your cart will have multiple fields of both <input name=FID> and <input name=FQty>, so that when the form is posted, Request.Form["FID"] and Request.Form["FQty"] will each be comma-separated lists? If so, maybe something like this will do the trick:

var FID = Request.Form["FID"].Split(',');
var FQTY = Request.Form["FQty"].Split(',');

for(int i = 0; i < FID.Length; ++i)
{
    String query = "INSERT INTO gah.orders VALUES ('" + m_OrderID + "','" + FID[i] + "', '" + FQTY[i] + "')";

    insD(db, query);
}

It’s a bit hard to understand with just the code you shared, but do let me know if I’m on the right track.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement