Using Datareader with label control

Posted on 2006-04-06
Last Modified: 2012-06-22
Hi EE Experts:

How do you bind data to a single label control?


<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs)

        If Not Page.IsPostBack Then
            Dim MyConnection As SqlConnection
            Dim MyCommand As SqlCommand
            Dim MyReader As SqlDataReader

            MyConnection = New SqlConnection()
            MyConnection.ConnectionString = _              

            MyCommand = New SqlCommand()
            MyCommand.CommandText = "SELECT ID, CustomerName FROM CUSTOMERS"
            MyCommand.CommandType = CommandType.Text
            MyCommand.Connection = MyConnection

            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)

           ??????????????????????  WHAT GOES HERE  ?????????????????????

        End If
    End Sub

Then, in the page:

<asp:Label ID="Label1" runat="server" Text="Label"><%# CustomerName %></asp:Label>

Is this possible in ASP.NET?  I don't want to use gridview, detailsview, formview, repeater, etc...  I just want to quickly bind a headline or single database field to a label or literal control.

Question by:jumpseatnews
    LVL 1

    Accepted Solution

    The simplest way is:

    If (MyReader.Read())
    Label1.Text = MyReader("CustomerName").ToString()

    In your aspx file:

    <asp:Label ID="Label1" runat="server" Text="Label1"></asp:Label>
    LVL 5

    Expert Comment

    gtmljsc  is correct.

    No points wanted but heres some advice Chris.

    You never have to use gridview, detailsview, formview, repeater, etc...  

    You can always read you data directly and put it to any control, as you can do any code you want between the reader

      MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
    Dim blah1 as string  = MyReader("Bah")
    Label1.text  = blah1
    'and so on
    end while

    You can aslo store your data in a dataset and call the data later in your page, so you don't have so much code in you reader, if you only have 1 row coming back then it makes more sense to assign the data to a class or local variables.

    Author Comment

    Thanks to you both!  This was exactly what I was looking for.

    EE Rocks!!!!!!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now