I am wanting to sort student enrollment by date in a database without using a grid. I can pull an individual student based on the enrollment date by using this code.
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled ='08/03/2012,' ");
This will successfully pull the student name from the database and when i use @name in my markup it supplies the name.
my question is, How can I pull multiple names without using a grid. I have tried this code
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled >'08/03/2012,' ");
This will only pull the first Student_Name that has a date greater than 08/03/2012. I have then tried this:
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled > '08/03/2012,' ");
var name1 = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled >'08/03/2012,' ");
that fixed sorting by date, but i am still getting only one name into the var "name"
Can more than one name be loaded into that variable? If not what could i do differently so that I can get multiple variables such as "name1" and "name2" that contain names of students enrolled after the defined date?
Thrasher114
Member
19 Points
25 Posts
Sorting Data by Date
Dec 11, 2012 01:20 AM|LINK
I am wanting to sort student enrollment by date in a database without using a grid. I can pull an individual student based on the enrollment date by using this code.
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled ='08/03/2012,' ");This will successfully pull the student name from the database and when i use @name in my markup it supplies the name.
my question is, How can I pull multiple names without using a grid. I have tried this code
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled >'08/03/2012,' ");This will only pull the first Student_Name that has a date greater than 08/03/2012. I have then tried this:
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled > '08/03/2012,' "); var name1 = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled >'08/03/2012,' ");both "name" and "name1" render the same result
oned_gk
All-Star
31764 Points
6492 Posts
Re: Sorting Data by Date
Dec 11, 2012 01:28 AM|LINK
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled >'08/03/2012' ORDER BY Date_Enrolled ASC");Thrasher114
Member
19 Points
25 Posts
Re: Sorting Data by Date
Dec 11, 2012 01:33 AM|LINK
Thanks for the quick response.
that fixed sorting by date, but i am still getting only one name into the var "name"
Can more than one name be loaded into that variable? If not what could i do differently so that I can get multiple variables such as "name1" and "name2" that contain names of students enrolled after the defined date?
chaaraan
Contributor
2170 Points
484 Posts
Re: Sorting Data by Date
Dec 11, 2012 02:30 AM|LINK
Hi
Use a datatable to return multiple values
datatable dtname = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled >'08/03/2012' ORDER BY Date_Enrolled ASC");
Regards,
charan
GmGregori
Contributor
5470 Points
737 Posts
Re: Sorting Data by Date
Dec 11, 2012 04:49 AM|LINK
You must use the Query() method instead of the QueryValue() method. Try
var name = db.Query(@"SELECT Student_Name FROM Student_Information WHERE Date_Enrolled >'08/03/2012' ORDER BY Date_Enrolled ASC");Pay attention that now the variable name is an IEnumerable and not a string.
ayanmesut
Member
219 Points
85 Posts
Re: Sorting Data by Date
Dec 11, 2012 05:42 AM|LINK
Add to GmGregori
The 'name' variable can hav emany names now. You can get them on the page:
@for(var row in name){ @row.Student_Name }Thrasher114
Member
19 Points
25 Posts
Re: Sorting Data by Date
Dec 20, 2012 09:45 PM|LINK
what Razor code do i use to display this?
Mikesdotnett...
All-Star
154951 Points
19870 Posts
Moderator
MVP
Re: Sorting Data by Date
Dec 21, 2012 04:29 AM|LINK
The code that ayanmesut posted. Did you try it?
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Thrasher114
Member
19 Points
25 Posts
Re: Sorting Data by Date
Dec 21, 2012 01:07 PM|LINK
yes ,but what goes inside the ( )? i put the exact code ayanmesut posted but an error occured.
GmGregori
Contributor
5470 Points
737 Posts
Re: Sorting Data by Date
Dec 21, 2012 01:27 PM|LINK
Try with something like:
<ul> @foreach(var row in name) { <li>@row.Student_Name</li> } </ul>