[RESOLVED] Proper Syntax for Storing a SQL SELECT Query to a String Variable?
Hey guys,
I've run into a situation where I simply need to take the value returned by my SQL SELECT statement and store that into a string variable. Until now my SELECT statements have just been used to populate datagrids. Here is the statement:
Code:
cmd2.CommandText = "SELECT custemail from customers where businessname='" & newTicket.ComboBox1.Text & "'"
What is the best way to store this value into a string variable for later use? I tried googling but everything I could find is more complicated than what I'm trying to do. Thanks!
Re: Proper Syntax for Storing a SQL SELECT Query to a String Variable?
Since you query for only 1 object (custemail), call ExecuteScalar on the command cmd2 and then test if it's not null, cast it to its true type and store in a variable.
Code:
cmd2.CommandText = "SELECT custemail from customers where businessname='" & newTicket.ComboBox1.Text & "'"
conn.Open()
Dim obj As Object = cmd2.ExecuteScalar()
conn.Close()
Dim custEmail As String = String.Empty
If obj IsNot Nothing Then
custEmail = CStr(obj)
End If
Re: Proper Syntax for Storing a SQL SELECT Query to a String Variable?
Thanks Stanav. I will do some more reading on ExecuteScalar. Repped!