By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.