[RESOLVED]Select Multiple into SQL Question

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!

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,

Dharmesh.Kotadiya

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?

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);
}

Mikesdotnetting

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

Leave a Reply