I need to make a website that pulls data from a SQL Server and displays it on the site, it is set up on a asp.net empty web application, and the pages are web forms so this code is in the .cs page.
How would I display this on data on the .aspx page ??
private List<Course> GetCourses()
{
var dataTable = new DataTable();
using (var sqlConnection = new SqlConnection("Data Source=localhost;Initial Catalog=tafe_work;Integrated Security=True"))
{
sqlConnection.Open();
using (var sqlCommand = new SqlCommand("select * from Course", sqlConnection))
{
using (var sqlReader = sqlCommand.ExecuteReader())
{
dataTable.Load(sqlReader);
}
}
}
var courses = new List<Course>();
foreach (DataRow dataRow in dataTable.Rows)
{
var course = new Course() {
ID = (int)dataRow["Course_ID"],
Name = (string)dataRow["Name"]
};
courses.Add(course);
}
return courses;
}
public class Course
{
public int ID { get; set; }
public string Name { get; set; }
}