I am working on a website which is developed in ASP.NET and backbend is C#. I am dynamically trying to pass the value from the Database to the header. I can do this for the user name by simply doing <%: Context.User.Identity.GetUserName() %>
and that will return the correct user name. How would I do this for a course name?
Below is my code which reads the values from the DB
public DataTable GetCourseData()
{
string UsrName = User.Identity.Name;
DataSet dt = new DataSet();
using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CourseName FROM Course WHERE UserName=@UserName"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
SqlParameter para2 = new SqlParameter("UserName", UsrName);
cmd.Parameters.Add(para2);
cmd.Connection = connection;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
}
return dt.Tables[0];
}
If I do <%: GetCourseData() %>
it doesn't return the course.
Any help would be much appreciated!
The problem you have is you return a DataTable
which describes a whole table with your query results. You probably want to get the data you need from it, like so:
return dt.Tables[0].Rows[0]["CourseName"];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.