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. Datagrid - Populate with SQL Select Statement

Datagrid - Populate with SQL Select Statement

Scheduled Pinned Locked Moved Visual Basic
cssdatabasehelp
4 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.
  • L Offline
    L Offline
    Larry White
    wrote on last edited by
    #1

    I am trying to populate a datagrid using a SQL select statement. The statement contains a "WHERE fieldname LIKE textbox.text" as below in my code. The problem is, I can't get the code right to display the data in the grid when the button is clicked. Here is my code so far: Public Sub loadDoc() Dim DAdapt As SqlDataAdapter Dim custMap As DataTableMapping = DAdapt.TableMappings.Add("Table", "DocumentList") custMap.ColumnMappings.Add("Title", "Title") custMap.ColumnMappings.Add("Expiration", "Expiration") custMap.ColumnMappings.Add("Keywords", "Keywords") Dim sql2 As String = "Select * FROM DocumentList WHERE Keywords LIKE '" & tbSearch.Text & "'" Dim ds As New DataSet DAdapt = New SqlDataAdapter(sql2, cnn1) Me.cnn1.Open() DAdapt.Fill(ds) Try grdDocumentList.DataSource = ds.DefaultViewManager ds.Clear() ds.Merge(ds) grdDocumentList.SetDataBinding(ds, "DocumentList") Catch eLoadMerge As System.Exception Throw eLoadMerge End Try Me.cnn1.Close() End Sub Thank you, :-D

    LWhite

    K 1 Reply Last reply
    0
    • L Larry White

      I am trying to populate a datagrid using a SQL select statement. The statement contains a "WHERE fieldname LIKE textbox.text" as below in my code. The problem is, I can't get the code right to display the data in the grid when the button is clicked. Here is my code so far: Public Sub loadDoc() Dim DAdapt As SqlDataAdapter Dim custMap As DataTableMapping = DAdapt.TableMappings.Add("Table", "DocumentList") custMap.ColumnMappings.Add("Title", "Title") custMap.ColumnMappings.Add("Expiration", "Expiration") custMap.ColumnMappings.Add("Keywords", "Keywords") Dim sql2 As String = "Select * FROM DocumentList WHERE Keywords LIKE '" & tbSearch.Text & "'" Dim ds As New DataSet DAdapt = New SqlDataAdapter(sql2, cnn1) Me.cnn1.Open() DAdapt.Fill(ds) Try grdDocumentList.DataSource = ds.DefaultViewManager ds.Clear() ds.Merge(ds) grdDocumentList.SetDataBinding(ds, "DocumentList") Catch eLoadMerge As System.Exception Throw eLoadMerge End Try Me.cnn1.Close() End Sub Thank you, :-D

      LWhite

      K Offline
      K Offline
      Kevin Nicol
      wrote on last edited by
      #2

      Hmmm, I have never done it like that, try something like this Dim DAdapt as new SqlDataAdapter DAdaPt.SelectCommand = new SqlCommand DAdapt.SelectCommand.CommandType = CommandType.Text DAdapt.SelectCommand.Connection = cnn1 DAdapt.SelectCommand.CommandText = "Select * FROM DocumentList WHERE Keywords LIKE '" & search.Text & "'" Dim ds as new Datatable("Display") try DAdapt.fill(ds) Catch ex as SQLException end try grdDocumentList.Datasource = ds

      L 2 Replies Last reply
      0
      • K Kevin Nicol

        Hmmm, I have never done it like that, try something like this Dim DAdapt as new SqlDataAdapter DAdaPt.SelectCommand = new SqlCommand DAdapt.SelectCommand.CommandType = CommandType.Text DAdapt.SelectCommand.Connection = cnn1 DAdapt.SelectCommand.CommandText = "Select * FROM DocumentList WHERE Keywords LIKE '" & search.Text & "'" Dim ds as new Datatable("Display") try DAdapt.fill(ds) Catch ex as SQLException end try grdDocumentList.Datasource = ds

        L Offline
        L Offline
        Larry White
        wrote on last edited by
        #3

        Sweet! Thank you very much, that worked. I appreciate the assistance.:cool:

        LWhite

        1 Reply Last reply
        0
        • K Kevin Nicol

          Hmmm, I have never done it like that, try something like this Dim DAdapt as new SqlDataAdapter DAdaPt.SelectCommand = new SqlCommand DAdapt.SelectCommand.CommandType = CommandType.Text DAdapt.SelectCommand.Connection = cnn1 DAdapt.SelectCommand.CommandText = "Select * FROM DocumentList WHERE Keywords LIKE '" & search.Text & "'" Dim ds as new Datatable("Display") try DAdapt.fill(ds) Catch ex as SQLException end try grdDocumentList.Datasource = ds

          L Offline
          L Offline
          Larry White
          wrote on last edited by
          #4

          I added another variation to this code that works very nicely to provide a search based on part of the "Keywords" field. Add this to the btnLoad_Click sub: tbSearch.Text = "%" & tbSearch.Text & "%" Again, Thanks to Kevin for the assist! Got the creative juices flowing again, lol.

          LWhite

          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