And that select list box obtain the data from the next sentences:
@{
var db= Database.Open("bdincubadora");
var sqlQ = "SELECT id_emprendedor, nombre_usuario, apellido_usuario, apellido2_usuario FROM emprendedor, usuario WHERE emprendedor.id_emprendedor=usuario.UserId ";
var data = db.Query(sqlQ);
}
Everything works fine until there. When i process the form (post), that is that happend:
if(IsPost){
equipoNombre=Request["nombre"];
equipoIntegrantes=Request["integrantes"];
if(equipoNombre.IsEmpty())
{
ModelState.AddError("nombre","Se necesita ingresar un nombre al Equipo");
}
if(equipoIntegrantes.IsEmpty())
{
ModelState.AddError("integrantes","Se necesita asignar integrantes al equipo");
}
if(ModelState.IsValid)
{
try
{
foreach(var usuarios in equipoIntegrantes)
{
//@usuarios
@*IN THE NEXT SECTION IS THE PROBLEM*@
var SQLINSERT2 = "INSERT INTO pertenece(nombre_equipo, id_emprendedor) VALUES (@0, @1)";
db.Execute(SQLINSERT2, equipoNombre, usuarios);
}
}
catch(Exception e)
{
//something
}
finally
{
}
}
} //end IsPost
And the result is incorrect. Jump the alert message, because i have a problem with the "foreign key". If i uncommented the "@usuarios", then show me a result, and this is a number, like 1, 2...etc, but, that is no his ID that i have in my DB, and only show
1 number (even if i select multiple items from the list). That is the problem.
if I delete what's inside the foreach, and uncommented the @usuarios, then display shows me the correct numbers (which are the identifiers used in the database).
So, ....how i can solve this problem? i need help please!
Afzaal, thanks so muh for answer. But, i have a problem: i need the "WHERE" clause in the query (or that condition). So, i can't delete that. any suggestions or idea?
Hopefully you can help me, because I really do not know how to solve this. Greetings, and already thanks.
Claudio
If you need a where clause you may try to use some other where clause. Like if you want to get a specific time and you are getting one value. You can use OR clause too. where you can try two methods.
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
It is, actually. The value being posted in Request["equipoIntegrantes"] is potentially a comma-separated string, and that cannot be inserted as a foreign key value. So the string needs to be converted to an array of individual values which is then iterated
so that each one is inserted separately.
Mike, your solution works excelent! thanks so mucho for your help. Now, i can insert in my DB without problem. Iam very happy!! :D really, thanks you!!! :D
skullsleep
Member
2 Points
14 Posts
Insert multiple selected items to SQLCE in Webmatrix and Razor
Nov 24, 2012 09:30 PM|LINK
Hello, my name is Claudio, and..in first place, i need to apologize for my english, but is very bad. So, i try to explain the best i can.
I tried inster in my DB all the multiple item selected from my select html element. my form is this:
<tr> <td><label for="user">Integrantes:</label></td> <td> <select name="integrantes" id="integrantes" multiple="Multiple"> <option value="" selected>--Seleccionar--</option> @foreach(var integ in data) { <option value="@integ.id_emprendedor">@integ.nombre_usuario @integ.apellido_usuario @integ.apellido2_usuario</option> } </select> </td> </tr>And that select list box obtain the data from the next sentences:
@{ var db= Database.Open("bdincubadora"); var sqlQ = "SELECT id_emprendedor, nombre_usuario, apellido_usuario, apellido2_usuario FROM emprendedor, usuario WHERE emprendedor.id_emprendedor=usuario.UserId "; var data = db.Query(sqlQ); }Everything works fine until there. When i process the form (post), that is that happend:
if(IsPost){ equipoNombre=Request["nombre"]; equipoIntegrantes=Request["integrantes"]; if(equipoNombre.IsEmpty()) { ModelState.AddError("nombre","Se necesita ingresar un nombre al Equipo"); } if(equipoIntegrantes.IsEmpty()) { ModelState.AddError("integrantes","Se necesita asignar integrantes al equipo"); } if(ModelState.IsValid) { try { foreach(var usuarios in equipoIntegrantes) { //@usuarios @*IN THE NEXT SECTION IS THE PROBLEM*@ var SQLINSERT2 = "INSERT INTO pertenece(nombre_equipo, id_emprendedor) VALUES (@0, @1)"; db.Execute(SQLINSERT2, equipoNombre, usuarios); } } catch(Exception e) { //something } finally { } } } //end IsPostAnd the result is incorrect. Jump the alert message, because i have a problem with the "foreign key". If i uncommented the "@usuarios", then show me a result, and this is a number, like 1, 2...etc, but, that is no his ID that i have in my DB, and only show 1 number (even if i select multiple items from the list). That is the problem.
if I delete what's inside the foreach, and uncommented the @usuarios, then display shows me the correct numbers (which are the identifiers used in the database).
So, ....how i can solve this problem? i need help please!
greetings, and already thanks.
Claudio
Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Insert multiple selected items to SQLCE in Webmatrix and Razor
Nov 25, 2012 07:27 AM|LINK
Its not an error in the Insert Into There is a clause you are using in the select statement.
You are using WHERE clause. This will only select lines (rows) which will meet the needs of your work. So remove it and you will get all results.
~~! FIREWALL !~~
skullsleep
Member
2 Points
14 Posts
Re: Insert multiple selected items to SQLCE in Webmatrix and Razor
Nov 26, 2012 02:02 AM|LINK
Afzaal, thanks so muh for answer. But, i have a problem: i need the "WHERE" clause in the query (or that condition). So, i can't delete that. any suggestions or idea?
Hopefully you can help me, because I really do not know how to solve this. Greetings, and already thanks.
Claudio
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Insert multiple selected items to SQLCE in Webmatrix and Razor
Nov 26, 2012 04:44 AM|LINK
foreach(var usuarios in equipoIntegrantes.Split(','))Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Insert multiple selected items to SQLCE in Webmatrix and Razor
Nov 26, 2012 10:40 AM|LINK
If you need a where clause you may try to use some other where clause. Like if you want to get a specific time and you are getting one value. You can use OR clause too. where you can try two methods.
~~! FIREWALL !~~
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Insert multiple selected items to SQLCE in Webmatrix and Razor
Nov 26, 2012 11:31 AM|LINK
It is, actually. The value being posted in Request["equipoIntegrantes"] is potentially a comma-separated string, and that cannot be inserted as a foreign key value. So the string needs to be converted to an array of individual values which is then iterated so that each one is inserted separately.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
skullsleep
Member
2 Points
14 Posts
Re: Insert multiple selected items to SQLCE in Webmatrix and Razor
Nov 26, 2012 06:50 PM|LINK
Mike, your solution works excelent! thanks so mucho for your help. Now, i can insert in my DB without problem. Iam very happy!! :D really, thanks you!!! :D