0

I am fresher in VB.NET and I am trying to execute an SQL query in VB.NET but not showing any value at output. Can u please help me to find where am I going Wrong.

Dim sConnectionString As String _
    = "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"

    Dim objConn As New SqlConnection(sConnectionString)
    objConn.Open()

    Dim sw ,readerObj
    Dim sSQL As String = "select top 1  " & sw & " = e.import  from tblrelcoms r , [beant].[dbo].tblequipments e where r.IDEquipment = e.IDEquipment"
    Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader
    Dim objCmd As New SqlCommand(sSQL, objConn)
    objCmd.ExecuteNonQuery()

    TextBox1.Text = sw.ToString()
5
  • 1
    Your var sw, you don't give it any value... Commented Mar 24, 2016 at 13:45
  • 2
    Turn on option strict Commented Mar 24, 2016 at 13:46
  • ExecuteNonQuery used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. Commented Mar 24, 2016 at 13:47
  • ok...But how to store a value of SQL query variable in the VB.NET variable Commented Mar 24, 2016 at 13:50
  • if readerObj.HasRows then readerObj.read() TextBox1.Text = rdr("yourcolumnname") something like that? Commented Mar 24, 2016 at 13:53

1 Answer 1

2

The problem you have is that you cannot just concatenate a variable into SQL and expect it to be updated once the SQL is executed.

ExecuteScalar is probably the easiest way of achieving what you want:

Dim sConnectionString As String _
    = "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"
    
Dim sSQL as string = "SELECT TOP 1 e.import  " _
                    "FROM   tblrelcoms r " & _
                    "       INNER JOIN [beant].[dbo].tblequipments e " & _
                    "           ON r.IDEquipment = e.IDEquipment " & _
                    "ORDER BY e.Import;"
                    
Using connection = new SqlConnection(sConnectionString)
    Using command = New SqlCommand(sSQL, connection)
        
        connection.Open()
        TextBox1.Text = command.ExecuteScalar().ToString()
        
    End Using
End Using

Although if you need more than one column, then you could use a data reader:

Dim sConnectionString As String _
    = "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"
    
Dim sSQL as string = "SELECT TOP 1 e.import  " _
                    "FROM   tblrelcoms r " & _
                    "       INNER JOIN [beant].[dbo].tblequipments e " & _
                    "           ON r.IDEquipment = e.IDEquipment " & _
                    "ORDER BY e.Import;"
                    
Using connection = new SqlConnection(sConnectionString)
    Using command = New SqlCommand(sSQL, connection)
        
        connection.Open()
        Using reader = command.ExecuteReader()
        
            If reader.Read()
                TextBox1.Text = reader.GetString(0)
            End If
        End Using
        
    End Using
End Using

I have made a couple of other changes too.

  1. Added Using blocks to ensure IDisposable objects are disposed of properly.
  2. Updated the sql join syntax from ANSI 89 implicit joins to ANSI 92 explicit joins, as the name suggests the syntax you are using is 24 years out of date. There are many reasons to start using the new syntax, which is nicely covered in this article: Bad habits to kick : using old-style JOINs
  3. Added an ORDER BY clause to your sql. TOP 1 without order by will give you indeterminate results (unless you only have one record, in which case top 1 is redundant)

A more complex solution would be to use output parameters, which will work and seems more in line with what you were originally trying to achieve, but is overkill for this situation (in my opinion):

Dim sConnectionString As String _
    = "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"
    
Dim sSQL as string = "SELECT TOP 1 @Output = e.import  " _
                    "FROM   tblrelcoms r " & _
                    "       INNER JOIN [beant].[dbo].tblequipments e " & _
                    "           ON r.IDEquipment = e.IDEquipment " & _
                    "ORDER BY e.Import;"
                    
Using connection = new SqlConnection(sConnectionString)
    Using command = New SqlCommand(sSQL, connection)
        
        connection.Open()
        Dim p As SqlParameter = command.Parameters.Add("@Output", SqlDbType.VarChar, 255)
        p.Direction = ParameterDirection.InputOutput
        
        command.ExecuteNonQuery();
        
        TextBox1.Text = p.Value.ToString()
        
    End Using
End Using

*Please excuse any syntax errors, I have not used VB.Net in years, and some c# quirks may be in the below, such as I can't remember if you just don't have to use parentheses for a parameterless method, or if you can't... Hopefully there is enough basic structure to get you started

Sign up to request clarification or add additional context in comments.

1 Comment

Great example, this one will work for the OP! Thank you for actually using using statements, there are too many times I do not see this...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.