I am having trouble with some code. I have created a form and all I want to do is run a query when the button is pressed and enter the outcome into the text box.
But I keep getting an error. Here is my code:
Public Sub getdn_Click(Query As Object, e As EventArgs) Handles getdn.Click
Try
SQLCon.Open()
SQLCmd = New SqlCommand(CStr(Query), SQLCon)
' build the query
SQLCmd.CommandText = "Select Top 1 docnumber+1 From transportreq Order By Docnumber Desc"
' run the query and obtain a reader to get the results
Dim R As SqlDataReader = SQLCmd.ExecuteReader()
' check if there are results
If (R.Read()) Then
' populate the values of the controls
dnText.Text = CStr(R(0))
End If
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
If SQLCon.State = ConnectionState.Open Then
SQLCon.Close()
End If
End Try
End Sub
Buttonto aString. ReplaceCStr(Query)with""and renameQuerytosender.