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. Database & SysAdmin
  3. Database
  4. how to bind the textbox value to sqlCommand?

how to bind the textbox value to sqlCommand?

Scheduled Pinned Locked Moved Database
tutorialquestion
3 Posts 3 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.
  • D Offline
    D Offline
    DotNet
    wrote on last edited by
    #1

    I have a textbox (txtPCID), button (btnSearch), and datagrid (DataGrid). Right now, I want to enter "P0002" in the textbox and click on the search button, I will get all the records which belong to PCID= P0002. Look at that 4th line. I don't know why the value in the textbox cannot pass to the sqlCommand? How to solve it? Sub BindDataGrid() Dim Tpcid As String Tpcid = txtPCID.Text cmdSql = New SqlCommand("Select * From Peripheral WHERE PCID=' & Tpcid & ' ", myConnection) myConnection.Open() DataGrid.DataSource = cmdSql.ExecuteReader() DataGrid.DataBind() myConnection.Close() End Sub

    S C 2 Replies Last reply
    0
    • D DotNet

      I have a textbox (txtPCID), button (btnSearch), and datagrid (DataGrid). Right now, I want to enter "P0002" in the textbox and click on the search button, I will get all the records which belong to PCID= P0002. Look at that 4th line. I don't know why the value in the textbox cannot pass to the sqlCommand? How to solve it? Sub BindDataGrid() Dim Tpcid As String Tpcid = txtPCID.Text cmdSql = New SqlCommand("Select * From Peripheral WHERE PCID=' & Tpcid & ' ", myConnection) myConnection.Open() DataGrid.DataSource = cmdSql.ExecuteReader() DataGrid.DataBind() myConnection.Close() End Sub

      S Offline
      S Offline
      Sudee
      wrote on last edited by
      #2

      cmdSql = New SqlCommand("Select * From Peripheral WHERE PCID='"+Tpcid+"' ", myConnection) I am not sure.. just try once. Sorry if I am wrong. :)- Sudeep ====== You need a head to program. Cool, fast and sharp.

      1 Reply Last reply
      0
      • D DotNet

        I have a textbox (txtPCID), button (btnSearch), and datagrid (DataGrid). Right now, I want to enter "P0002" in the textbox and click on the search button, I will get all the records which belong to PCID= P0002. Look at that 4th line. I don't know why the value in the textbox cannot pass to the sqlCommand? How to solve it? Sub BindDataGrid() Dim Tpcid As String Tpcid = txtPCID.Text cmdSql = New SqlCommand("Select * From Peripheral WHERE PCID=' & Tpcid & ' ", myConnection) myConnection.Open() DataGrid.DataSource = cmdSql.ExecuteReader() DataGrid.DataBind() myConnection.Close() End Sub

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        You should really parameterise your command. What you have here, if it worked, is suseptable to an injection attack.

        cmdSql = New SqlCommand("Select * From Peripheral WHERE PCID=@PCID", myConnection)
        cmdSql.Parameters.Add("@PCID",SqlDbType.VarChar, 10).Value = Tpcid

        The above is much safer. (Remember to change the SqlDbType and field length values as appropriate)


        "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!

        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