0 Replies - 1148 Views - Last Post: 10 July 2014 - 07:39 AM Rate Topic: -----

#1 jimjackuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-June 14

Webmatrix 3

Posted 10 July 2014 - 07:39 AM

My basic question is how can I take the selected value in a list and use that value in a SQL INSERT Statement. I have 2 tables- Contacts and Salutation. When a record is to be inserted, a Salutation(Mr, Mrs,Dr etc) is selected from a list.For ease I have included only the code dealing with this. The code actually works as is, however , I feel my solution is suspect. Here is the code:-
@{
    var Saln="";


<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>

   <Form  method="post" action="">
   <select name="Choice"> 

 @{      
    var db1 = Database.Open("Contact");
    var selectCommand1 = "SELECT ID, Saln FROM Salutation";
    var selectedData1 = db1.Query(selectCommand1);

    foreach(var row1 in selectedData1)
     {
        <option value="@row1.ID">@row1.Saln</option>
     }
   } 
    </select> 
  @{      
   if (Request["Choice"] != null)
   {
   var  Sal=Request["Choice"];
   var db = Database.Open("Contacts");
   var insertCommand = "INSERT INTO Contacts (Saln) VALUES( "+Request["Choice"]+')';
    db.Execute(insertCommand,Saln );

   }  
  }
  <input type="submit" value="Submit" /> 
  </form>
  </body>
  </html>


The variable Sal or @0 is not accepted. Your advice would be appreciated. Regards Jim

Is This A Good Question/Topic? 0
  • +

Page 1 of 1