Results 1 to 2 of 2

Thread: retrieving data

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    166

    retrieving data

    Hi,

    I have the follwoing code

    vb Code:
    1. Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
    2.                                                             "Data Source=" & DBPath & ";")
    3.         Dim Comm As System.Data.OleDb.OleDbCommand
    4.  
    5.         Dim sqlcom As String
    6.  
    7.         If Me.cmb_client.Text <> "" Then
    8.             sqlcom = "Select * from tbl_client where client_id = " & Me.cmb_client.SelectedValue
    9.             MessageBox.Show(sqlcom)
    10.             Comm = New System.Data.OleDb.OleDbCommand(sqlcom, Conn)
    11.             'defines new data adapter
    12.             Dim DA As New System.Data.OleDb.OleDbDataAdapter
    13.             DA.SelectCommand = Comm
    14.  
    15.             ' defines new dataset
    16.             Dim DS As New DataSet
    17.  
    18.             Try
    19.                 DA.Fill(DS)
    20.             Catch ex As Exception
    21.                 'The user may have entered a string as their login id
    22.                 MessageBox.Show(Err.Description)
    23.                 Me.Close()
    24.  
    25.                 Exit Sub
    26.             End Try
    27.  
    28.             Comm.Dispose()
    29.             DA.Dispose()
    30.             Conn.Close()
    31.             Conn.Dispose()
    32.  
    33.         Else
    34.  
    35.         End If
    how do i make it populate the data into my text fields with the data?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: retrieving data

    Firstly, do NOT call Exit Sub in the Catch block UNLESS you perform cleanup in a Finally block. What happens to your connection if that line is hit? It stays open. You should be closing and disposing in a Finally block to ensure it gets done no matter what. That Exit Sub is also useless because there is no code to be executed after that except the required cleanup anyway.

    As for your question, assuming that you're using VB 2005 (please always specify when you post) you should add a BindingSource to your form and bind it to all your controls in the designer. Then all you need to do at run time is bind your DataTable to the BindingSource.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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