Re: Reading Single Value from a SQL DB [FIXED]

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



sloan wrote:
You want to look at the ExecuteScalar method:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatasqlclientsqlcommandclassexecutescalartopic.asp

Its a cheaper operation than ExecuteReader, when you know you only want 1
value.


Thanks. Your link didn't work but I went to http://msdn.microsoft.com/library/default.asp and did a search for ExecuteScalar and found what I needed (probably where you intended me to go).


My resulting code:

Dim strConnection As String = "server=localhost;uid=sa;pwd=password;database=tickets"
Dim objConnection As New SqlConnection(strConnection)
Dim strResult As String
Dim strSQL As String = "SELECT field2 FROM TTickets WHERE PK = " & txtID.Text
Dim objCommand As New SqlCommand(strSQL, objConnection)
objCommand.Connection.Open()
strResult = objCommand.ExecuteScalar().ToString
objConnection.Close()
Response.Write(strResult)


Thanks everyone for the quick responses.

Jim
.



Relevant Pages