Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7033165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:01:47+00:00 2026-05-28T01:01:47+00:00

I have a sample code snippet below which works just fine (I trimmed the

  • 0

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>
               <%}
           }
       %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:01:48+00:00Added an answer on May 28, 2026 at 1:01 am

    You could try the code below.

    in your aspx markup add a Label control

    <asp:Label id="container" runat="server"/>
    

    In your codebehind make use of your datareader at the point where you need to .

     while (myreader.Read())
               {
                   if (myreader["subid"] != null)
                   {
                       string str = string.Format("<div>{0}</div>",myreader["subid"]);
                       container.Controls.Add(new LiteralControl(str)); //label we added in markup
                   }
           }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample code which doesn't seem to want to run. import
I have this snippet from apple sample code LazyTableImages . In the code below
Does anyone have sample code, or a tutorial which demonstrates how to use Grappa
I have a plot (sample code pasted below) that I am trying to add
I have a little piece of code which reads a file and i want
In my little code snippet below I have a wrapper class for a simple
Does anyone have sample code to copy open (in-use and locked by another program)
I have downloaded sample code from Apple Center.I also have gone through following question:
I have this sample code for async operations (copied from the interwebs) public class
Does anyone have some sample code showing how to POST to a URL using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.