I have a sample code snippet below which works just fine (I trimmed the query a bit for sample purposes). I want to display the data in an ASPX file but I do not want to use a repeater, datagrid or anything like that. I should mention I am not working in MVC3.
In ASP 3.0 it was possible to use a DO WHILE loop to do this, I can’t seem to get it to work in ASPX. Can someone post a code snippet that will do the job? Many thanks in advance.
public void sqlconn2(object sender, EventArgs e)
{
SqlConnection cn = null;
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT DISTINCT submajor.subid FROM subdetails", cn);
myreader = cmd.ExecuteReader();
}
public static SqlDataReader myreader { get; set; }
Edit: If I put the code directly in the markup, it works. Obviously I do not want to do this. Here is the working sample of the markup code:
<%
System.Data.SqlClient.SqlConnection cn = null;
cn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
cn.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT DISTINCT submajor.subid FROM subdetails", cn);
myreader = cmd.ExecuteReader();
%>
<%
while (myreader.Read())
{
if (myreader["subid"] != null)
{%>
<div><%Response.Write(myreader["subid"]);%></div>
<%}
}
%>
You could try the code below.
in your aspx markup add a Label control
In your codebehind make use of your datareader at the point where you need to .