Results 1 to 2 of 2

Thread: Get two fields from a record in VB.Net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Get two fields from a record in VB.Net

    I have been working with VB demos / tutorials for a while now and am now working on a real project.

    I have managed a database connection but am unable to figure out how to get the value of a field from my table based on the employee number.

    Here is my code so far:
    Code:
    Dim strFname As String
    Dim strLname As String
    Dim cnn As SqlConnection = _
    New SqlConnection("workstation id=XXXXX;packet" & _
       " size=4096;integrated security=SSPI;data source=XXXXX;" & _
       "persist security info=False;initial catalog=DBName")
    Dim cmdSelect As New SqlCommand
    cnn.Open()
    
    Try
       cmdSelect = cnn.CreateCommand
       cmdSelect.CommandText = "SELECT Name From PREH where Employee=" & txtEmp.Text
    
    Catch ex As Exception
       MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Error")
    End Try
    My form has two text boxes one called txtEmpNum and the other txtEmpName. I am trying to write code that will return the name of the employee to the name text box on the LostFocus event which is where that code above is from. Any help would be appreciated.

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    It doesn't look like you're actually executing the command.

    Don't you need to do something like this? (copied from MSDN)

    VB Code:
    1. Public Sub ReadMyData(myConnString As String)
    2.     Dim mySelectQuery As String = "SELECT OrderID, Customer FROM Orders"
    3.     Dim myConnection As New SqlConnection(myConnString)
    4.     Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    5.     myConnection.Open()
    6.     Dim myReader As SqlDataReader = myCommand.ExecuteReader()
    7.     Try
    8.         While myReader.Read()
    9.             Console.WriteLine((myReader.GetInt32(0).ToString & ", " & myReader.GetString(1)))
    10.         End While
    11.     Finally
    12.         ' always call Close when done reading.
    13.         myReader.Close()
    14.         ' always call Close when done reading.
    15.         myConnection.Close()
    16.     End Try
    17. End Sub 'ReadMyData
    This world is not my home. I'm just passing through.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width