Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. WHERE clause of SELECT command

WHERE clause of SELECT command

Scheduled Pinned Locked Moved Visual Basic
helpsecuritytutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raabi Anony
    wrote on last edited by
    #1

    Hello everybody! A newer problem on a newer day :) This time, I am facing a very basic issue in the following code:

    Private Function GetDataSource(strCampusName) As DataTable 
        Dim cmdText As String = "SELECT \* from tblStaffInfo tbl WHERE tbl.CampusName = " + strCampusName 
        Dim conxnString As String = "Data Source=Raabi\\SQLEXPRESS; 
         Initial Catalog=StaffReport; Integrated Security=True"
        Dim DGVadapter As New SqlDataAdapter()
        Dim ds As New DataSet()
        Dim conxn As New SqlConnection(conxnString)
        Dim cmd As SqlCommand = conxn.CreateCommand()
        Try
            cmd.CommandText = cmdText
            DGVadapter.SelectCommand = cmd
            conxn.Open()
            DGVadapter.Fill(ds)
            Return ds.Tables(0)
            conxn.Close()
        Catch ex As Exception
            MsgBox("Error: " & ex.Message)
        End Try
    End Function
    

    Error: Invalid column name 'whatever' Even if I use, for example;

    Dim cmdText As String = "SELECT * from tblStaffInfo tbl WHERE tbl.CampusName = CityCampus"

    I receive the same error. Any help, please!

    R 1 Reply Last reply
    0
    • R Raabi Anony

      Hello everybody! A newer problem on a newer day :) This time, I am facing a very basic issue in the following code:

      Private Function GetDataSource(strCampusName) As DataTable 
          Dim cmdText As String = "SELECT \* from tblStaffInfo tbl WHERE tbl.CampusName = " + strCampusName 
          Dim conxnString As String = "Data Source=Raabi\\SQLEXPRESS; 
           Initial Catalog=StaffReport; Integrated Security=True"
          Dim DGVadapter As New SqlDataAdapter()
          Dim ds As New DataSet()
          Dim conxn As New SqlConnection(conxnString)
          Dim cmd As SqlCommand = conxn.CreateCommand()
          Try
              cmd.CommandText = cmdText
              DGVadapter.SelectCommand = cmd
              conxn.Open()
              DGVadapter.Fill(ds)
              Return ds.Tables(0)
              conxn.Close()
          Catch ex As Exception
              MsgBox("Error: " & ex.Message)
          End Try
      End Function
      

      Error: Invalid column name 'whatever' Even if I use, for example;

      Dim cmdText As String = "SELECT * from tblStaffInfo tbl WHERE tbl.CampusName = CityCampus"

      I receive the same error. Any help, please!

      R Offline
      R Offline
      Raabi Anony
      wrote on last edited by
      #2

      Sorry everybody, I was committing a syntax error. It must be:

      Dim cmdText As String = "SELECT * from tblStaffInfo tbl WHERE tbl.CampusName = '" & strCampusName & "'"

      It is resolved.

      Richard DeemingR 1 Reply Last reply
      0
      • R Raabi Anony

        Sorry everybody, I was committing a syntax error. It must be:

        Dim cmdText As String = "SELECT * from tblStaffInfo tbl WHERE tbl.CampusName = '" & strCampusName & "'"

        It is resolved.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]

        Private Function GetDataSource(strCampusName) As DataTable
        Dim conxnString As String = "Data Source=Raabi\SQLEXPRESS;Initial Catalog=StaffReport; Integrated Security=True"
        Dim cmdText As String = "SELECT * from tblStaffInfo tbl WHERE tbl.CampusName = @CampusName"

        Using conxn As New SqlConnection(conxnString)
            Using cmd As New SqlCommand(cmdText, conxn)
                cmd.Parameters.AddWithValue("@CampusName", strCampusName)
                
                Dim DGVadapter As New SqlDataAdapter(cmd)
                Dim ds As New DataSet()
                DGVadapter.Fill(ds)
                Return ds.Tables(0)
            End Using
        End Using
        

        End Function


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups