3 Replies - 1185 Views - Last Post: 20 October 2014 - 04:35 AM Rate Topic: -----

#1 FloweredHat   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-October 14

SQL Query only returns one item (rather than complete list) - asp.net

Posted 20 October 2014 - 03:50 AM

I have an SQL query within ASP.NET, but it only seems to return 1 result rather than filling the list with 4 as it should (as the query itself is correct)

Code:

List<string> dropItems, dropItems2;

using (var dbContext = new DatabaseContext())
{
    dropItems = dbContext.Database.SqlQuery<String>(
        String.Format("SELECT locations.Code FROM locations")).ToList();
}
using (var dbContext = new DatabaseContext())
{
    dropItems2 = dbContext.Database.SqlQuery<String>(
        String.Format("SELECT locations.Name FROM locations")).ToList();
}



After the code executes, each list only contains one item rather than 4 which they both should

Is This A Good Question/Topic? 0
  • +

Replies To: SQL Query only returns one item (rather than complete list) - asp.net

#2 DarenR   User is offline

  • D.I.C Lover

Reputation: 595
  • View blog
  • Posts: 3,839
  • Joined: 12-January 10

Re: SQL Query only returns one item (rather than complete list) - asp.net

Posted 20 October 2014 - 03:57 AM

you should use a foreach

foreach( string result in results){

yadda
yadda

}

Was This Post Helpful? 0
  • +
  • -

#3 FloweredHat   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-October 14

Re: SQL Query only returns one item (rather than complete list) - asp.net

Posted 20 October 2014 - 04:00 AM

View PostDarenR, on 20 October 2014 - 03:57 AM, said:

you should use a foreach

foreach( string result in results){

yadda
yadda

}



Where should I use that. I assumed by making one list equal to the newly created list it would be filled correctly. How do you suggest I modify this to include the above?
Was This Post Helpful? 0
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 595
  • View blog
  • Posts: 3,839
  • Joined: 12-January 10

Re: SQL Query only returns one item (rather than complete list) - asp.net

Posted 20 October 2014 - 04:35 AM

here is a good example from microsoft http://msdn.microsof...=vs.110%29.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1