473,513 Members | 4,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with assigning data from data reader to label control in web form

i'm trying to read data from SQL Server database using
data reader and assigned it to a label in my asp.net web
application. but when the button is clicked, nothing
appears. please help, thanks.

Private Sub btnTesting_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnTesting.Click
Dim cn As New SqlClient.SqlConnection
Dim cm As New SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader
cn.ConnectionString = _
"data source=MHNAZLY;initial catalog=MEDICAL;"
Try
cn.Open()
Try
cm.Connection = cn
cm.CommandText = "SELECT StaffID,
StaffName, Designation, Contact_no, Email FROM
Admin_Staff WHERE(StaffID='1001104214')"
dr = cm.ExecuteReader()
Try
While dr.Read()

'lblDoctorID.Text = dr.Item
("StaffID").ToString()
lblDoctorID.Text = dr.GetString(1)
End While
Finally
dr.Close()
End Try
Finally
cn.Close()
End Try
Catch ex As Exception

End Try
End Sub
..
Nov 17 '05 #1
1 1688
Hi,

Please try the following. It uses the datasource property rather than
looping through the records in the datareader.

If Not IsPostBack Then
Bind()
End If
End Sub

Private Sub Bind()
Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim queryString As String = "SELECT au_id, au_lname, au_fname FROM
authors"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Qry1 =
sqlCommand.ExecuteReader(System.Data.CommandBehavi or.CloseConnection)
DropDownList1.DataSource = Qry1
DropDownList1.DataTextField = "au_id"
DropDownList1.DataBind()
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
DropDownList1.Items.Insert(0, "ALL")
DropDownList1.Items.Insert(0, "")
DropDownList1.ClearSelection()
End Sub
Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "mhnazly" <an*******@discussions.microsoft.com>
Sender: "mhnazly" <an*******@discussions.microsoft.com>
Subject: problem with assigning data from data reader to label control in web form Date: Sat, 25 Oct 2003 17:35:14 -0700
Lines: 41
Message-ID: <02****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcObWQVe1AXyDwbyS6qJsfXHSc9x1A==
Newsgroups: microsoft.public.dotnet.framework.aspnet
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186570
NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

i'm trying to read data from SQL Server database using
data reader and assigned it to a label in my asp.net web
application. but when the button is clicked, nothing
appears. please help, thanks.

Private Sub btnTesting_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnTesting.Click
Dim cn As New SqlClient.SqlConnection
Dim cm As New SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader
cn.ConnectionString = _
"data source=MHNAZLY;initial catalog=MEDICAL;"
Try
cn.Open()
Try
cm.Connection = cn
cm.CommandText = "SELECT StaffID,
StaffName, Designation, Contact_no, Email FROM
Admin_Staff WHERE(StaffID='1001104214')"
dr = cm.ExecuteReader()
Try
While dr.Read()

'lblDoctorID.Text = dr.Item
("StaffID").ToString()
lblDoctorID.Text = dr.GetString(1)
End While
Finally
dr.Close()
End Try
Finally
cn.Close()
End Try
Catch ex As Exception

End Try
End Sub
.


Nov 17 '05 #2

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

Similar topics

0
2214
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These controls and validators are then added to a table for display to the user. This code did seem to work originally, but now does not amd I am stumped as to...
1
2506
by: Joe Bloggs | last post by:
I am trying display the contents of a table in a web page, select certain rows from that table and then display the fields that I have selected (now table columns) as text in a Label object. Amazingly I have managed to display the table - no problem, I can select rows - no problem, BUT I can't work out how to display more than one of the...
9
2180
by: Nathan Sokalski | last post by:
I am trying to connect to a Microsoft Access Database from my ASP.NET Application. I use the following code to create my connection string: cmdSelect.Connection = New System.Data.OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATASOURCE=" & Server.MapPath("/WebApplication1/nathansokalski_com.mdb")) Although the error does not...
2
6533
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row *that is going to be deleted* into another table, before the original data is deleted. I am trying to use the RowDeleting method to call an Update...
30
4519
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired...
0
1742
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a .TXT file with Keys/Values
6
6512
by: Bill | last post by:
Hi All, New to the whole .Net and C# thing but trying. In classic asp this was simple to fill a table with dynamic content and hyperlinks. Here is an example of what I am trying to do in classic asp: <table width="640" border="0" cellspacing="3" cellpadding="3"> <tr class="mcsmalltextbold"> <td>First Name</td> <td>Last Name</td>...
3
1447
by: amjad905 | last post by:
Hi, I have created some dynamic labels and some dynamic linkbuttons... those linkbuttons have addhandlers assigned to them... but in postback those buttons gets deleted... the code is shown below... plz help asap... Dim conn As New Data.OleDb.OleDbConnection(System.Configuration.ConfigurationManager.AppSettings("connectionstring")) ...
2
2613
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to update the information which is stored in a SQL database. In testing we noticed that the form was updating correctly but the update mechanism was also...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.