Results 1 to 8 of 8

Thread: Listbox problem, information to textboxes...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Listbox problem, information to textboxes...

    I'm selecting a customer from a listbox of surnames...

    I need to address to display, and the date arrived back...

    Can't get anything to go into the textboxes, if dr.hasrows is showing up as none I think... not sure how to go about remedey"ing" it though...

    Listbox Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.         Dim ConnectionString As String
    3.         Dim SQLString As String
    4.         Dim cmd As System.Data.OleDb.OleDbCommand
    5.         Dim conn As System.Data.OleDb.OleDbConnection
    6.         Dim dr As System.Data.OleDb.OleDbDataReader
    7.         ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data "
    8.         ConnectionString += "Source=" & "Opticians.accdb "
    9.         conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
    10.         AddressTextBox.Text = ListBox1.Text 'Gives the AddressTextBox the value that we click in the ListBox
    11.         SQLString = "SELECT * FROM CustomerTable " ' Selects all from CustomerTable
    12.         SQLString += "INNER JOIN SpectacleSalesTable ON CustomerTable.CustomerID = SpectacleSalesTable.CustomerID " 'Bring EyeTestTable in, using the records where the CustomerID matches up in both tables
    13.         SQLString += "Where '" & AddressTextBox.Text & "'" 'If the value that we clicked in the listbox, matches the surname it will try to connect
    14.         SQLString += "= Surname"
    15.         Try
    16.             conn.Open()
    17.             If ConnectionState.Open.ToString = "Open" Then
    18.                 cmd = New System.Data.OleDb.OleDbCommand(SQLString, conn)
    19.                 dr = cmd.ExecuteReader()
    20.                 If dr.HasRows Then
    21.                     dr.Read()
    22.                     If Not IsDBNull(dr.Item("SpecSalesTable.FrameID")) Then
    23.                         DepositTextBox.Text = dr.Item("SpecSalesTable.FrameID").ToString 'Adds the CustomerID to the CustomerIDTextBox
    24.                     End If
    25.                     If Not IsDBNull(dr.Item("SpecSalesTable.DateReceived")) Then
    26.                         DateReceivedTextBox.Text = dr.Item("SpecSalesTable.DateReceived").ToString 'Adds the EyeTestID to the EyeTestIDTextBox
    27.                     End If
    28.                     If Not IsDBNull(dr.Item("CustomerTable.Street")) Then
    29.                         AddressTextBox.Text = dr.Item("CustomerTable.Street").ToString 'Adds the Street to the AddressTextBox
    30.                     End If
    31.                     If Not IsDBNull(dr.Item("CustomerTable.Town")) Then
    32.                         Address2TextBox.Text = dr.Item("CustomerTable.Town").ToString 'Adds the Town to the Address2TextBox
    33.                     End If
    34.                     If Not IsDBNull(dr.Item("CustomerTable.County")) Then
    35.                         Address3TextBox.Text = dr.Item("CustomerTable.County").ToString 'Adds the County to the Address3TextBox
    36.                     End If
    37.                 End If
    38.             End If
    39.         Catch ex As Exception
    40.         End Try
    41.     End Sub

  2. #2
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: Listbox problem, information to textboxes...

    Quote Originally Posted by Kielo View Post
    Can't get anything to go into the textboxes, if dr.hasrows is showing up as none I think...
    Well, thats your problem right there. If you have no rows then the code that fills in the textboxes doesn't execute. Your going to have to find out why the database isn't returning anything. Then worry about displaying the data.

  3. #3
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: Listbox problem, information to textboxes...

    A real quick glance at your WHERE statement... It looks incorrect to me. You have:
    vb Code:
    1. SQLString += "Where '" & AddressTextBox.Text & "'" 'If the value that we clicked in the listbox, matches the surname it will try to connect
    2. SQLString += "= Surname"

    Don't you want something like:
    vb Code:
    1. SQLString += "Where Surname = '" & AddressTextBox.Text & "'"

  4. #4

    Re: Listbox problem, information to textboxes...

    WOW... There is some serious unesessary work in this procdure. I will help as best I can and I'm sure others can add more.. Firstly, only connect to your DB once in the session, then close it as your session closes. By which I mean open it with your app, close it as your app closes. Place the follwing into a module with a function name and call the function when required. As it currently stands your are trying to perform a DB connection everytime the listbox item is selected (you will need to do a little work with the function).

    Code:
            Dim ConnectionString As String
            Dim SQLString As String
            Dim cmd As System.Data.OleDb.OleDbCommand
            Dim conn As System.Data.OleDb.OleDbConnection
            Dim dr As System.Data.OleDb.OleDbDataReader
            ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data "
            ConnectionString += "Source=" & "Opticians.accdb "
            conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
    conn.open
    You do not need this

    Code:
     AddressTextBox.Text = ListBox1.Text 'Gives the AddressTextBox the value that we click in the ListBox
    You are just moving the value from one place to another, simply reference listbox1.text

    Simplify your SQL query.. e.g. SQLString = "SELECT * from tableA WHERE Field1 = somedata AND fieldb = moredata"

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Listbox problem, information to textboxes...

    Working on that stuff now, thanks for the comments

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Listbox problem, information to textboxes...

    Hmm... still can't figure out where the original problem is, I have this working in a different setting, using different tables... but this one wont work

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Listbox problem, information to textboxes...

    Open once on app start is VB6 (ADO) style recommenations. For ADO.Net the recommenation is open and close on use.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Listbox problem, information to textboxes...

    Okay... anyone spotted anything else?

Tags for this Thread

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