473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Best way to display a label with text from db?

Hi,

If I want to display some text value coming out of a database on an aspx
page (on a label), do I really have to create a connection and command
object etc. all manually (such as in the code below) or is there an easier
way (asp.net 2.0, VS 2005) ?

Thanks for your help!

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrin gs("DevDbConnectionString"
).ToString)
If Not Page.IsPostBack Then
Dim strQuery As String = "SELECT CategoryID FROM Category WHERE
CategoryURL='" + Page.Request("dir") + "'"
Dim myCommand As New SqlCommand(strQuery, myConnection)
myConnection.Open()
Dim strCategoryID As String
strCategoryID = myCommand.ExecuteScalar
myConnection.Close()
End If
[..]
End Sub
--
Kees de Winter
Mar 31 '06 #1
1 1545


At the very very least, I'd create a WebUserControl (ascx) file. to
encapsulate the logic.

You can then drag and drop these onto your form when you need it.

I personally don't like writing data access code in the persentation
layer...but you'll have decide what to do there.

Right Click... Add New Item ... Web User Control

Drag a label onto it.

create a Public Property .. to use as you key (or "dir" in your case)

...

If you want shorter lined dataaccess code, considere the EnterpriseLibrary
Data Access block. search msdn and you'll find it.

it has a small learning curve, but the payoff is well worth it. You could
get your code down to 1 to 3 lines to get your value.

"Kees de Winter" <nospam> wrote in message
news:44**********************@text.nova.planet.nl. ..
Hi,

If I want to display some text value coming out of a database on an aspx
page (on a label), do I really have to create a connection and command
object etc. all manually (such as in the code below) or is there an easier
way (asp.net 2.0, VS 2005) ?

Thanks for your help!

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrin gs("DevDbConnectionString" ).ToString)
If Not Page.IsPostBack Then
Dim strQuery As String = "SELECT CategoryID FROM Category WHERE CategoryURL='" + Page.Request("dir") + "'"
Dim myCommand As New SqlCommand(strQuery, myConnection)
myConnection.Open()
Dim strCategoryID As String
strCategoryID = myCommand.ExecuteScalar
myConnection.Close()
End If
[..]
End Sub
--
Kees de Winter

Mar 31 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Agency | last post by:
I'm still working on the bpm counter. I need to have at least 2 displays that are not static. One would be a clock/running time and the other would should the current beat count. How would I...
2
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server...
2
by: anthonysmales | last post by:
I have created a web-service which returns a line of HTML. I have created a web-application which calls the web-service and then sets the Label1.Text property to the output of the web-service, in...
7
by: jez123456 | last post by:
Hi, I have the following method where I need to display which database is being processed, however, the label lblDatabase dosn't seem to work until the end. private void...
1
by: Thubaiti | last post by:
Hi, I have an image web control with other web controls.. <asp:Image id="itemImage" runat="server"></asp:Image> <asp:Label id="itemNameLabel" runat="server"></asp:Label> <asp:Label...
3
by: CG3000 | last post by:
creating a label to be printed out on a label printer. The image on the label would be a jpeg file with labels and text beside that image. This image and text would be printed out by a printer...
1
by: rbinington | last post by:
Hi, I am trying to write a DNN module that has the ability to insert articles into an article repository. I want the users to be able to move pages around and enter text into the FCKEditor. I...
2
by: heinz | last post by:
What control is best to display ordinary text on an aspx web page? For example, a short newspaper article as the main content. In html is just in a table, but whats the best way of doing it in...
1
by: fsalvador | last post by:
Hi, I need help with this. I am trying to figure it out but I can't. I have 2 pages. I would like to display in the second page the content of one class and hide the content of the other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.