jumpseatnews
asked on
Using Datareader with label control
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.ConnectionStr ing = _
ConfigurationManager.Conne ctionStrin gs("DSN_No rthwind"). Connection String
MyCommand = New SqlCommand()
MyCommand.CommandText = "SELECT ID, CustomerName FROM CUSTOMERS"
MyCommand.CommandType = CommandType.Text
MyCommand.Connection = MyConnection
MyCommand.Connection.Open( )
MyReader = MyCommand.ExecuteReader(Co mmandBehav ior.CloseC onnection)
?????????????????????? 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
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.ConnectionStr
ConfigurationManager.Conne
MyCommand = New SqlCommand()
MyCommand.CommandText = "SELECT ID, CustomerName FROM CUSTOMERS"
MyCommand.CommandType = CommandType.Text
MyCommand.Connection = MyConnection
MyCommand.Connection.Open(
MyReader = MyCommand.ExecuteReader(Co
?????????????????????? 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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to you both! This was exactly what I was looking for.
EE Rocks!!!!!!
EE Rocks!!!!!!
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(Co
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.