?
Solved

Using Datareader with label control

Posted on 2006-04-06
3
Medium Priority
?
330 Views
Last Modified: 2012-06-22
Hi EE Experts:

How do you bind data to a single label control?

Code:

<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 = _              
       ConfigurationManager.ConnectionStrings("DSN_Northwind").ConnectionString

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

            MyCommand.Connection.Open()
            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)

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

            MyCommand.Dispose()
            MyConnection.Dispose()
        End If
    End Sub
</script>

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.

Chris
0
Comment
Question by:jumpseatnews
3 Comments
 
LVL 1

Accepted Solution

by:
gtmljsc earned 2000 total points
ID: 16397050
The simplest way is:

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

In your aspx file:

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

Expert Comment

by:Jojo1771
ID: 16397091
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)
While MyReader.read
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.
0
 

Author Comment

by:jumpseatnews
ID: 16397614
Thanks to you both!  This was exactly what I was looking for.

EE Rocks!!!!!!

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 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