Link to home
Start Free TrialLog in
Avatar of fuzzymallets1
fuzzymallets1Flag for United States of America

asked on

Assigning SQL query result to a variable in VB.net

I am trying to assign a SQL query result to a variable in VB.Net

Here is the code I am using (without success)
        Dim cmd2 As New SqlCommand
        Dim SONum As String
        cmd2.CommandText = "SELECT SONum From foy.ticket where TicketNum = " & ticket & ""
         SONum = cmd2.CommandText
     
When I run it I get exactly "SELECT SONum From test.ticket where TicketNum = " & ticket & "" and not the query result. I know why I'm getting the full command, but I want the query result.

Any Ideas how this can be done in VB.NET?
Avatar of SQL_SERVER_DBA
SQL_SERVER_DBA
Flag of United States of America image


SqlCommand cmd = new SqlCommand("SELECT SONum From foy.ticket where TicketNum = " & ticket & "", conn)
SqlDataReader rdr = cmd.ExecuteReader()
dont forget to assign your connection string for the second arguement( ie conn)
Here is the basic structure, I got from Microsoft.
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.connection(vs.71).aspx

[Visual Basic]
Public Sub CreateMySqlCommand()
    Dim mySelectQuery As String = "SELECT * FROM Categories ORDER BY CategoryID"
    Dim myConnectString As String = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"
    Dim myCommand As New SqlCommand(mySelectQuery)
    myCommand.Connection = New SqlConnection(myConnectString)
    myCommand.CommandTimeout = 15
    myCommand.CommandType = CommandType.Text
End Sub 'CreateMySqlCommand
Avatar of fuzzymallets1

ASKER

Ok I understand where the first part goes and to change the conn to my SQLconn, but where does the SqlDataReader rdr = cmd.ExecuteReader() go?
OK I think I know what you where talking about. Is this what you mean?

Private Sub ReadOrderData(ByVal connectionString As String)
        Dim ticket As String = TxtTicket.Text
        Dim queryString As String = "SELECT SONum From foy.ticket where TicketNum = " & ticket & ""
        Using Sqlconn As New SqlConnection(connectionString)
            Dim command As New SqlCommand(queryString, Sqlconn)
            Dim rdr As SqlDataReader = command.ExecuteReader()
            Dim SONum As String
           While rdr.Read()
                Console.WriteLine(String.Format("{0, {1", _
                    rdr(0), rdr(1)))
            End While
            rdr.Close()
        End Using
    End Sub

OK, know how does this add the query result to a variable?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Chris. This is what I ended up doing with your help.

Dim ticket As String = TxtTicket.Text
        Dim queryString As String = "SELECT SONum From foy.ticket where TicketNum = " & ticket & ""

        Using SqlConn As New SqlConnection(connectionString)
            ' Define the command
            Dim cmd As New SqlCommand(queryString, SqlConn)
            Dim da As New SqlClient.SqlDataAdapter(cmd)
            Dim ds As New DataSet
            da.Fill(ds, "SONum")
            Dim dt As DataTable = ds.Tables("SONum")
            Dim dv As DataView = dt.DefaultView
            Txttest.DataBindings.Add("text", dv, "SONum")
        End Using