Results 1 to 4 of 4

Thread: Select statement that uses a variable, result to a text box

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    2

    Select statement that uses a variable, result to a text box

    Hi,

    I'm relatively new to VB.
    I want to perform a SELECT statement that uses a variable to return a value, example;

    mysql> SELECT room_description FROM room_descriptions WHERE current_room = 'someVariable';

    and display the result in a TextBox i.e. TextBox.text = someVariable.

    So how do I perform a query from VB that passes a variable?
    How do I assign the result to a variable?

    Also, is there an excellent book or web site the demonstrates all these things in simple examples?

    Many thanks,

    Ted.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Select statement that uses a variable, result to a text box

    Welcome to the forums.

    Change myConnection to whatever you are using as your connection object.

    SqlCommand and SqlDataReader are only valid for SQL Server. If that is what you are using, then you are good. If not, then they need to be changed as appropriate.
    Code:
    Dim sSQL As String
    sSQL = "SELECT room_description  "   
    sSQL = sSQL & "FROM room_descriptions "
    sSQL = sSQL & "WHERE current_room  = @Room "
    Dim command As New SqlCommand(sSQL, myConnection)        
    command.Parameters.AddWithValue("@Room", txtRoom.Text)       
    Dim reader As SqlDataReader = command.ExecuteReader()
    reader.Read()
    TextBox1.Text = reader.GetString(0)
    reader.Close()

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Select statement that uses a variable, result to a text box

    It would actually be better to call ExecuteScalar, which is intended for queries that return a single value, rather than ExecuteReader. The rest would be the same but this part:
    Code:
    Dim reader As SqlDataReader = command.ExecuteReader()
    reader.Read()
    TextBox1.Text = reader.GetString(0)
    would become:
    Code:
    TextBox1.Text = CStr(command.ExecuteScalar())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    2

    Re: Select statement that uses a variable, result to a text box

    Hi,

    many thanks to you both for the replies.
    I'll give this a go and get back to you.

    Cheers,

    Ted.

Posting Permissions

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



Click Here to Expand Forum to Full Width