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

M

mhnazly

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
..
 
M

Mike Moore [MSFT]

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(connectionString)
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.CommandBehavior.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.


--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top