views:

23

answers:

1

I have a column in a table that contains a message and I want this message to be displayed in a label. Here is my code which currently doesn't populate the label.

Protected conString As String = ConfigurationManager.AppSettings("sqldirectory")
Dim cnn As New SqlConnection(conString)
    Dim cmd As New SqlCommand("select message from [database].[dbo].[table]")
    Dim dr As SqlDataReader

    Try
        cnn.Open()
        dr = cmd.ExecuteReader()
        lblMsg.Text = dr(0).ToString
    Catch ex As Exception
        dr = Nothing
    Finally
        cnn.Close()
    End Try
A: 

Ok, after playing around, I came up with this which works

Protected conString As String = ConfigurationManager.AppSettings("sqldirectory")
Dim cnn As New SqlConnection(conString)
Dim cmd As New SqlCommand("select message from [database].[dbo].[table]", cnn)
Dim dr As SqlDataReader
Dim msg As String
Try
    cnn.Open()
    dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    While dr.Read()
        msg = dr("message")
        lblMsg.Text = msg
    End While
Catch ex As Exception
    dr = Nothing
Finally
    cnn.Close()
End Try
mjw06d