locked
Select Multiple into SQL Question RRS feed

  • Question

  • User-2051275383 posted

    Afternoon guys,

    I am hoping this is a simple question!

    I have a select multiple field in my form:

    <select multiple="" name="formSelectThings">
    <option value="Bob">Bob Smith</option>
    <option value="Fred">Fred Jones</option>
    <option value="John">John Doe</option>
    </select>

    I want to insert each item that is selected into a table - What is the best way of going about this? I have figured out how to combine each value and insert however I am wanting them to be separate items in a table.

    Cheers!

    Monday, October 6, 2014 10:47 PM

Answers

  • User-821857111 posted

    When the form is posted, the items will be available in Request["formSelectThings"] as a comma-separated string. If you want to insert a new row per item, you have to use string.Split to generate an array from the string and then loop through that, inserting as you go:

    foreach(var item in Request["formSelectThing"].Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)){
        var sql = "INSERT INTO MyTable (MyField) VALUES (@0)"
        db.Execute(sql, item);
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 7, 2014 2:25 AM

All replies

  • User-1353043493 posted

    Hi,

    There is so many way to achieve this requirement.

    1) Insert comma(,) separated value in table

    2) create child table i.e one to many  (master child table)

    Thanks,

    Tuesday, October 7, 2014 1:28 AM
  • User-2051275383 posted

    Hi,

    There is so many way to achieve this requirement.

    1) Insert comma(,) separated value in table

    2) create child table i.e one to many  (master child table)

    Thanks,



    I already have a table structure - I am just not sure of the best method to insert them into the database?

    Tuesday, October 7, 2014 2:18 AM
  • User-821857111 posted

    When the form is posted, the items will be available in Request["formSelectThings"] as a comma-separated string. If you want to insert a new row per item, you have to use string.Split to generate an array from the string and then loop through that, inserting as you go:

    foreach(var item in Request["formSelectThing"].Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)){
        var sql = "INSERT INTO MyTable (MyField) VALUES (@0)"
        db.Execute(sql, item);
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 7, 2014 2:25 AM
  • User-2051275383 posted

    When the form is posted, the items will be available in Request["formSelectThings"] as a comma-separated string. If you want to insert a new row per item, you have to use string.Split to generate an array from the string and then loop through that, inserting as you go:

    foreach(var item in Request["formSelectThing"].Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)){
        var sql = "INSERT INTO MyTable (MyField) VALUES (@0)"
        db.Execute(sql, item);
    }




    I was literally 30 seconds from posting!!

    I found this which has helped but you have fast tracked it (once again) Mike!! 

    http://forums.asp.net/t/1861123.aspx?Insert+multiple+selected+items+to+SQLCE+in+Webmatrix+and+Razor

    Tuesday, October 7, 2014 2:45 AM