locked
Webform RRS feed

  • Question

  • User1481030129 posted

    Hi,

    I have some customers in my DB and I would like to be able to select a customer in a webform when a customer places a new order. The customers are already in my DB and I use the following <select option> in my webform:

    <select class="Select" name="CustomersList">
           @foreach(var row in selectedData)
           {
           <option value="@row.CustomerID">@row.CompanyName</option>
           }
      </select>

    When selecting the option in the list, I would like to have the customer number inserted into the order DB in order to have a link between the customer and the order placed, but it seems that the customer is not inserted into table Orders, when I select the customer name from the <select option>.

    Can someone help me what I'm doing wrong?

    Thanks for your support!

    bartvn

    Monday, April 28, 2014 4:31 PM

Answers

  • User938738290 posted

    That would be up to the folks in your DB thread.  So long as your CustomerID variable has the correct value in the debugger after you click submit, the form is working properly.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 1, 2014 4:34 PM

All replies

  • User938738290 posted

    The HTML <select> element doesn't actually interact directly with the database.  You'll need to put the select inside an HTML <form> element.  When the form is submitted, you'll be able to retrieve the submitted values from the Request and insert them into the DB.

    Monday, April 28, 2014 4:39 PM
  • User1481030129 posted

    Emu42,

    Thanks for the reply.

    My <select> code is in an HTML form element, but it seems that the selection is not capturing the selection itself.
    I hereby add an example of my code:

    @{
         Layout = "~/_SiteLayout.cshtml"; 
        
    <!-- Verify if user is logged-in and if not redirect -->
    if (!WebSecurity.IsAuthenticated) { 
         Response.Redirect("login.cshtml");
    }
     
     <!-- Set Date Time -->
    var currentDateTime = DateTime.Now;  
     
     
    var customers = Database.Open("EmptySite");
        var selectCommand = "SELECT CustomerID, CompanyName FROM tblCustomers";
        var selectedData = customers.Query(selectCommand);
     
    }
    <form action="" method="post">
    <table>
    <tr><td>
        Customer:
    </td><td>
    <tr><td>
      <select class="Select" name="CustomersList">
           @foreach(var row in selectedData)
           {
           <option value="@row.CustomerID">@row.CompanyName</option>
           }
      </select>
    </td></tr>
           
    <tr><td>
        Ref. number:
    </td><td>
    <input type="text" name="RefNumber" size="50"/>
    </td></tr>
    <tr><td>
       Job number :
    </td><td>
    <input type="text" name="JobNumber" size="50"/>
    </td></tr>
    <tr><td>
    <input type="submit" value="" />
    </td><td></td></tr>
    </table>
    </form> <br>
    </body> </html>
    Any help would be welcome!
    Tuesday, April 29, 2014 10:03 AM
  • User938738290 posted

    emu42

    When the form is submitted, you'll be able to retrieve the submitted values from the Request and insert them into the DB.

    Where is the part of your code where you do the above?  Microsoft offers a tutorial on inserting form information into a database here: http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2/entering-data.

    EDIT: Link wasn't a link.  Fixed that.

    Tuesday, April 29, 2014 1:20 PM
  • User1481030129 posted

    Emu42,

    Please find hereby my code page:

    @{
         Layout = "~/_SiteLayout.cshtml"; 
        Page.Title = "Application x ";
     
    <!-- Verify if user is logged-in and if not redirect -->
    if (!WebSecurity.IsAuthenticated) { 
         Response.Redirect("login.cshtml");
    }
     
     <!-- Set Date Time -->
    var currentDateTime = DateTime.Now;  
     
    <!-- Open customer DB-->
    var customers = Database.Open("EmptySite");
        var selectCommand = "SELECT CustomerID, CompanyName FROM tblCustomers ORDER BY CompanyName";
        var selectedData = customers.Query(selectCommand);
     
        if(IsPost){
        var CustomerID=Request["CustomerID"];
        var RefNumber=Request["RefNumber"];
        var JobNumber=Request["JobNumber"];
        var WorkOrderDate=Request["WorkOrderDate"];
        var WorkOrderType=Request["WorkOrderType"];
        var Employee=Request["Employee"];
        var VehiculeType=Request["VehiculeType"];
        var SubVehiculeType=Request["SubVehiculeType"];
        var NumberPlate=Request["NumberPlate"];
        var ChassisNumber=Request["ChassisNumber"];
        var TruckNumber=Request["TruckNumber"];
        var ContainerNumber=Request["ContainerNumber"];
        var TaskDescription=Request["TaskDescription"];
        var ConstructionType=Request["ConstructionType"];
        var WorkedHours=Request["WorkedHours"];
        var UnitPrice=Request["UnitPrice"];
        var Quantity=Request["Quantity"];
        var Material=Request["Material"];
        var UnitPriceMaterial=Request["UnitPriceMaterial"];
        var Quantity2=Request["Quantity2"];
        var Material2=Request["Material2"];
        var UnitPriceMaterial2=Request["UnitPriceMaterial2"];
        var Quantity3=Request["Quantity3"];
        var Material3=Request["Material3"];
        var UnitPriceMaterial3=Request["UnitPriceMaterial3"];
        var Quantity4=Request["Quantity4"];
        var Material4=Request["Material4"];
        var UnitPriceMaterial4=Request["UnitPriceMaterial4"];
        var Quantity5=Request["Quantity5"];
        var Material5=Request["Material5"];
        var UnitPriceMaterial5=Request["UnitPriceMaterial5"];
        var Quantity6=Request["Quantity6"];
        var Material6=Request["Material6"];
        var UnitPriceMaterial6=Request["UnitPriceMaterial6"];
        var Total=Request["Total"];
        var TotalWorkOrder=Request["TotalWorkOrder"];
        var insertQueryString = "INSERT INTO tblWorkOrders " +
        "(CustomerID, RefNumber, JobNumber, WorkOrderDate, WorkOrderType, Employee, VehiculeType, SubVehiculeType, NumberPlate, ChassisNumber, TruckNumber, ContainerNumber, TaskDescription, ConstructionType, WorkedHours, UnitPrice, Quantity, Material, UnitPriceMaterial, Quantity2, Material2, UnitPriceMaterial2, Quantity3, Material3, UnitPriceMaterial3, Quantity4, Material4, UnitPriceMaterial4, Quantity5, Material5, UnitPriceMaterial5, Quantity6, Material6, UnitPriceMaterial6, Total, TotalWorkOrder) " +
        "VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19; @20, @21, @22, @23, @24, @25, @26, @27, @28, @29, @30, @31, @32, @33, @34, @35)";
        var db = Database.Open("EmptySite");
        db.Execute(insertQueryString, CustomerID, RefNumber, JobNumber, WorkOrderDate, WorkOrderType, Employee, VehiculeType, SubVehiculeType, NumberPlate, ChassisNumber, TruckNumber, ContainerNumber, TaskDescription, ConstructionType, WorkedHours, UnitPrice, Quantity, Material, UnitPriceMaterial, Quantity2, Material2, UnitPriceMaterial2, Quantity3, Material3, UnitPriceMaterial3, Quantity4, Material4, UnitPriceMaterial4, Quantity5, Material5, UnitPriceMaterial5, Quantity6, Material6, UnitPriceMaterial6, Total, TotalWorkOrder);
        Response.Redirect("WorkOrderConfirmation.cshtml");
        }
    }
     
    <h3>New Application</h3>
     
        <br>
        <h5>Customer:</h5>    
        
    <form action="" method="post">
    <table>
    <tr><td>
         
      <select class="Select" name="CustomersList">
        <option value="">-- Customer --</option>       
          @foreach(var row in selectedData)
           {
           <option value="@row.CustomerID">@row.CompanyName</option>
           }
      </select>
    </td></tr>
    <tr><td>
        Reference number:
    </td><td>
    <input type="text" name="RefNumber" size="50"/>
    </td></tr>
    <tr><td>
       Job number :
    </td><td>
    <input type="text" name="JobNumber" size="50"/>
    </td></tr>
    <tr><td>
        Date:
    </td><td>
    <input type="text" name="WorkOrderDate" size="20" placeholder="01/01/2014"/>
    </td></tr>
    <tr><td>
    <input type="submit" value="Werkbon opslagen" />
    </td><td></td></tr>
          </table>
       </form>
      <br>       
     </body>
    </html>
    
     
    Thursday, May 1, 2014 3:37 PM
  • User938738290 posted

    bartvn

    var CustomerID=Request["CustomerID"];

    bartvn

    <select class="Select" name="CustomersList">

    The name of your <select> element does not match the name of the parameter you're trying to pull from the Request.

    Thursday, May 1, 2014 3:58 PM
  • User1481030129 posted

    emu42,

    Thanks for the feedback.
    I just modified the name towards CustomerID in the <select> element.

    I tried to insert the data & receive the following error:
    There was an error parsing the query. [ Token line number = 1,Token line offset = 619,Token in error = ; ]

    Line 57: "VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19; @20, @21, @22, @23, @24, @25, @26, @27, @28, @29, @30, @31, @32, @33, @34, @35)"; Line 58: var db = Database.Open("EmptySite"); Line 59: db.Execute(insertQueryString, CustomerID, RefNumber, JobNumber, WorkOrderDate, WorkOrderType, Employee, VehiculeType, SubVehiculeType, NumberPlate, ChassisNumber, TruckNumber, ContainerNumber, TaskDescription, ConstructionType, WorkedHours, UnitPrice, Quantity, Material, UnitPriceMaterial, Quantity2, Material2, UnitPriceMaterial2, Quantity3, Material3, UnitPriceMaterial3, Quantity4, Material4, UnitPriceMaterial4, Quantity5, Material5, UnitPriceMaterial5, Quantity6, Material6, UnitPriceMaterial6, Total, TotalWorkOrder); Line 60: Response.Redirect("WorkOrderConfirmation.cshtml"); Line 61: }

    Thursday, May 1, 2014 4:05 PM
  • User938738290 posted

    That would be up to the folks in your DB thread.  So long as your CustomerID variable has the correct value in the debugger after you click submit, the form is working properly.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 1, 2014 4:34 PM