Results 1 to 3 of 3

Thread: [RESOLVED] Proper Syntax for Storing a SQL SELECT Query to a String Variable?

  1. #1
    Lively Member
    Join Date
    Apr 10
    Posts
    66

    Resolved [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!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 06
    Location
    Providence, RI - USA
    Posts
    9,167

    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
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Lively Member
    Join Date
    Apr 10
    Posts
    66

    Re: Proper Syntax for Storing a SQL SELECT Query to a String Variable?

    Thanks Stanav. I will do some more reading on ExecuteScalar. Repped!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •