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. Read a record in SQL and .NET

Read a record in SQL and .NET

Scheduled Pinned Locked Moved Visual Basic
databasetutorialcsharp
2 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.
  • G Offline
    G Offline
    gollnick
    wrote on last edited by
    #1

    I can fill a datagrid from SQL.. I can do this with ADO/JET but I am at a loss on how to get a single record to display on my form from an SQL database using my current connection. I am looking for a clear and simple example. Thanks Gollnick

    J 1 Reply Last reply
    0
    • G gollnick

      I can fill a datagrid from SQL.. I can do this with ADO/JET but I am at a loss on how to get a single record to display on my form from an SQL database using my current connection. I am looking for a clear and simple example. Thanks Gollnick

      J Offline
      J Offline
      John Honan
      wrote on last edited by
      #2

      There should be lots of examples out there of how to return multiple records, looping through records, returning one record etc. But here's a simple example of how I get back a record from a SELECT statement: Private Function GetCustName(ByVal CustNum As String) As String Dim strConn As String = ConfigurationSettings.AppSettings.Item("connectionstring") 'Read connection string from web.config Dim conSQL As New SqlConnection(strConn) 'Create the connection Dim cmdSQL As New SqlCommand("SELECT CustName FROM Customers WHERE CustNum ='" & CustNum & "'", conSQL) 'The query I want to run Dim Rdr As SqlDataReader conSQL.Open() 'Open the connection Rdr = cmdSQL.ExecuteReader() 'Execute the query Rdr.Read() 'Read the record (only returns ONE record!) GetCustName = IIf(IsDBNull(Rdr("CustName")), "", Rdr("CustName")) 'Read the value from a field called 'CustName' conSQL.Close() End Function This function returns a value from a record when you pass it a search criteris. If you want to populate text boxes on your form, then after the 'Rdr.Read()' line, you could do something like: myName.text = Rdr("Name") myAddress1.text = Rdr("Address1") myAddress2.text = Rdr("Address2") etc... Hope this helps, Regards, John. www.silveronion.com[^]

      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