Results 1 to 14 of 14

Thread: Displaying database information problem...

  1. #1

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

    Displaying database information problem...

    ok Code:
    1. Public Class ReceiptSpectacles
    2.  
    3.     Private Sub ReceiptSpectacles_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         DisplayList(5) 'LOADS DISPLAYLIST METHOD
    5.     End Sub
    6.  
    7.     Private Sub DisplayList(ByVal CusRef As Integer)
    8.         Dim ConnectionString As String
    9.         Dim SQLString As String
    10.         Dim TitleString As String
    11.         Dim conn As System.Data.OleDb.OleDbConnection
    12.         Dim dr As System.Data.OleDb.OleDbDataReader
    13.         Dim cmd As System.Data.OleDb.OleDbCommand
    14.         ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;data "
    15.         ConnectionString += "Source=" & "Opticians.accdb "
    16.         conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
    17.         SQLString = "SELECT * FROM SpecSalesTable" 'SELECTS ALL INFORMATION
    18.         Try                                     '    FROM DATABASE
    19.             conn.Open()
    20.             If ConnectionState.Open Then
    21.                 cmd = New System.Data.OleDb.OleDbCommand(SQLString, conn)
    22.                 dr = cmd.ExecuteReader()
    23.                 If dr.HasRows Then
    24.                     FramesListBox.Items.Clear()
    25.                     While dr.Read
    26.                         If Not IsDBNull(dr.Item("FrameID")) Then  'INSERTS INFORMATION INTO
    27.                             TitleString = dr.Item("FrameID")      '       LISTBOX
    28.                             FramesListBox.Items.Add(TitleString)
    29.                         End If
    30.                     End While
    31.                 End If
    32.                 dr.Close()
    33.             End If
    34.         Catch
    35.             MessageBox.Show("Error accessing database")     'IF ERROR ENCOUNTERED SHOWS MESSAGE
    36.         End Try
    37.         conn.Close()
    38.     End Sub
    39.  
    40.     Private Sub FramesListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FramesListBox.SelectedIndexChanged
    41.         Dim ConnectionString As String
    42.         Dim SQLString As String
    43.         Dim cmd As System.Data.OleDb.OleDbCommand
    44.         Dim conn As System.Data.OleDb.OleDbConnection
    45.         Dim dr As System.Data.OleDb.OleDbDataReader
    46.         ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data "
    47.         ConnectionString += "Source=" & "Opticians.accdb "
    48.         conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
    49.         TextBox1.Text = FramesListBox.Text
    50.         SQLString = "SELECT * FROM SpecSalesTable "
    51.         SQLString += "INNER JOIN CustomerTable ON SpecSalesTable.CustomerID = CustomerTable.CustomerID "
    52.         SQLString += "Where '" & TextBox1.Text & "'"
    53.         SQLString += "= FrameID"
    54.         Try
    55.             conn.Open()
    56.             If ConnectionState.Open.ToString = "Open" Then
    57.                 cmd = New System.Data.OleDb.OleDbCommand(SQLString, conn)
    58.                 dr = cmd.ExecuteReader()
    59.                 [I]If dr.HasRows Then[/I]
    60.                     dr.Read()
    61.                     If Not IsDBNull(dr.Item("FrameID")) Then
    62.                         TextBox1.Text = dr.Item("FrameID").ToString
    63.                     End If
    64.                     If Not IsDBNull(dr.Item("CustomerID")) Then
    65.                         TextBox2.Text = dr.Item("CustomerID").ToString
    66.                     End If
    67.                     If Not IsDBNull(dr.Item("Surname")) Then
    68.                         TextBox3.Text = dr.Item("Surname").ToString
    69.                     End If
    70.                     If Not IsDBNull(dr.Item("Firstname")) Then
    71.                         TextBox3.Text = dr.Item("Firstname").ToString
    72.                     End If
    73.  
    74.                 End If
    75.             End If
    76.         Catch ex As Exception
    77.         End Try
    78.     End Sub


    Right, there's my code...

    A listbox, 3 text boxes.

    The listbox should display the FrameID which is located in the SpecSalesTable

    Then I want, the surname, firstname from the Customer table to be displayed, provided the customer table. customerID = specsalestable.customerID

    Any ideas why this isn't working?

    I italic'd a part
    "If dr.HasRows Then"

    Thats showing up as not having rows, so it's not even trying to do the add to textbox part...


    Thanks

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

    Re: Displaying database information problem...

    I see a test to see if the connection is Open. If open you do something.... OK where is it opened?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

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

    Re: Displaying database information problem...

    Sorry I'm confused... where is the connection opened?

    I'm lost

  4. #4
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: Displaying database information problem...

    Okay, consider this section of your code:

    Code:
    Try                          '    FROM DATABASE
    conn.Open()
    If ConnectionState.Open Then
    You're opening the connection, okay, but the if-statement will always evaluate to true. Try this instead:

    Code:
    If conn.State = ConnectionState.Open Then
    I would, however, expect a failed connection to raise an exception...

    Also, in your FramesListBox_SelectedIndexChanged handler, I don't see where you close the connection.

  5. #5

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

    Re: Displaying database information problem...

    Alright, changed that... still didn't fix anything... (well make it work)

    I just need surname, firstname from the customer table but not getting anything =/
    Last edited by Kielo; Mar 10th, 2010 at 03:35 PM.

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

    Re: Displaying database information problem...

    How about adding a messagebox to the catch statement here and see if an error message pops out?

    vb.net Code:
    1. Try
    2.             conn.Open()
    3.             If ConnectionState.Open.ToString = "Open" Then
    4.                 cmd = New System.Data.OleDb.OleDbCommand(SQLString, conn)
    5.                 dr = cmd.ExecuteReader()
    6.                 [i]If dr.HasRows Then[/i]
    7.                     dr.Read()
    8.                     If Not IsDBNull(dr.Item("FrameID")) Then
    9.                         TextBox1.Text = dr.Item("FrameID").ToString
    10.                     End If
    11.                     If Not IsDBNull(dr.Item("CustomerID")) Then
    12.                         TextBox2.Text = dr.Item("CustomerID").ToString
    13.                     End If
    14.                     If Not IsDBNull(dr.Item("Surname")) Then
    15.                         TextBox3.Text = dr.Item("Surname").ToString
    16.                     End If
    17.                     If Not IsDBNull(dr.Item("Firstname")) Then
    18.                         TextBox3.Text = dr.Item("Firstname").ToString
    19.                     End If
    20.  
    21.                 End If
    22.             End If
    23.         Catch ex As Exception
    24.              MessageBox.Show(ex.Message)
    25.         End Try
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

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

    Re: Displaying database information problem...

    Type mismatch in expression

  8. #8

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

    Re: Displaying database information problem...

    Strange, I can't see the mismatch... there just names... is it looking for integers I wonder?

    How do I check that?

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

    Re: Displaying database information problem...

    The problem is in you SQL Statement.

    Is FrameId a string or Number?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10

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

    Re: Displaying database information problem...

    FrameID = Text field in Access...


    There is a number in there, but im not saving it as integer or anything, just as text.

    SQLString += "INNER JOIN CustomerTable ON SpecSalesTable.CustomerID = CustomerTable.CustomerID "

    CustomerTable.CustomerID = Primary key in that table...

    How do I resolve that? Assuming thats the problem?

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

    Re: Displaying database information problem...

    Do you know how to set degubing on? Can you get the actual SQL statement that is generated and passed to the database and post it here. Also the table structure of all the tables involved (espically the columns in the Joins and the Where clasuse)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  12. #12

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

    Re: Displaying database information problem...

    I don't know how to set debugging on... how do I got about doing that?

    Thanks dude

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

    Re: Displaying database information problem...

    Place a break point on a line. The process will stop when the break point is reached and you can step though the code. You can see the value of different variables.

    You need to read and understand debuging before you get much futher in programing.


    Start here:
    http://msdn.microsoft.com/en-us/library/sc65sadd.aspx
    Last edited by GaryMazzone; Mar 11th, 2010 at 07:32 PM.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  14. #14

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

    Re: Displaying database information problem...

    Ah yeah, I've done that quite a few times... Just wan't fully sure if thats what you meant...

    Thanks

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