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

Discussion in 'ASP .Net' started by mhnazly, Oct 26, 2003.

  1. mhnazly

    mhnazly Guest

    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
    ..
    mhnazly, Oct 26, 2003
    #1
    1. Advertising

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


    --------------------
    > Content-Class: urn:content-classes:message
    > From: "mhnazly" <>
    > Sender: "mhnazly" <>
    > 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: <027301c39b59$05616750$>
    > 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
    > .
    >
    >
    >
    Mike Moore [MSFT], Oct 28, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guilherme Grillo

    reader inside a reader

    Guilherme Grillo, Nov 7, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    516
    sloan
    Nov 7, 2007
  2. Rahul Chatterjee
    Replies:
    1
    Views:
    724
    Rahul Chatterjee
    Sep 25, 2008
  3. Pokkai Dokkai
    Replies:
    1
    Views:
    226
    Hassan Schroeder
    Mar 24, 2008
  4. weston
    Replies:
    1
    Views:
    238
    Richard Cornford
    Sep 22, 2006
  5. Knut Krueger
    Replies:
    8
    Views:
    252
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page