Showing a sql query result

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dougancil
    Contributor
    • Apr 2010
    • 347

    Showing a sql query result

    I have the following code in my project:

    Code:
    rivate Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim connectionString As String
            Dim cnn As SqlConnection
            Dim myCommand As SqlCommand
            'the connection string to the SQL server'
            connectionString = "Data Source=10.2.1.41;Initial Catalog=MDR;uid=xxxx;password=xxxxxxxx"
            cnn = New SqlConnection(connectionString)
            cnn.Open()
            'the SQL query'
            myCommand = New SqlCommand("select payrolldate from payroll where payrolldate <= getdate (), select dateadd (day, 7, Getdate())")
            cnn.Close()
            Dim ButtonDialogResult As DialogResult
            ButtonDialogResult = MessageBox.Show("The next date available to you is", "Payroll", MessageBoxButtons.YesNo)
            If ButtonDialogResult = Windows.Forms.DialogResult.Yes Then
                Button2.Enabled = True
                Button1.Enabled = False
            End If
        End Sub
    End Class
    And what I'm needing to do is to show in my messagebox.show the date from the sql query that I ran before this. I know that I havent declared a variable in that statement but I'm having a bit of a lapse today and was just wondering how to show that result in my messagebox.show .

    Thanks

    Doug
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    You'll have to execute the SqlCommand you create at line 10 and put that in a string.
    Code:
    cnn.Open() 
    'the SQL query' 
    myCommand = New SqlCommand("select payrolldate from payroll where payrolldate <= getdate (), select dateadd (day, 7, Getdate())") 
    Dim strResult as String = myCommand.ExecuteScalar()
    cnn.Close()
    Your value (if your statement only generates one value, otherwise you need to put it in a DataTable) is now in strResult and that's what you can add to your Msgbox.

    Steven

    Comment

    Working...