Is there a way to send the results of a sql statement to a messagebox?
Printable View
Is there a way to send the results of a sql statement to a messagebox?
There are many ways. But what do you mean, exactly?
Is there just one result, like a sum or max? Or does it return a table?
Just one value, no tables.
Dim sqlCmd as new SqlClient.SqlCommand
Dim Result as Integer (or string, or decimal, whatever the result will be)
SqlCmd.CommandText = "Your query here"
sqlCmd.Connection = YourSQLConnection
YourSQLConnection.Open()
Result = sqlCmd.ExecuteScalar
YourSQLConnection.Close()
TextBox1.Text = Result.ToString
This is pretty much just psuedocode, and I typed it straight into the editor. But the ExecuteScalar part is what I think you are after. After you have it stored in Result (or whatever you name your variable) it is very easy to put it in a textbox, or do whatever else you want to do with it.
Oh, and the open, executescalar, and close lines need to be put in a try/catch block so your program doesn't crash if there is an error.
Exaclty what I needed! Thank You!