Results 1 to 4 of 4

Thread: [RESOLVED] datagrid and datareader problem...

  1. #1

    Thread Starter
    Registered User
    Join Date
    Nov 2005
    Posts
    206

    Resolved [RESOLVED] datagrid and datareader problem...

    ok, so im trying to execute a query based on the row that a user clicks on...

    now for the purpose of testing, the contact_id is set to one, just to ensure that when the code runs it gets an actual data row...

    heres my code, that selects an entire row, then simply changes the text on a themed box control...
    VB Code:
    1. Private Sub dtaGridContacts_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtaGridContacts.MouseUp
    2.  
    3.         Dim rowPoint = New Point(e.X, e.Y)
    4.         Dim hti As DataGrid.HitTestInfo = dtaGridContacts.HitTest(rowPoint)
    5.         If hti.Type = DataGrid.HitTestType.Cell Then
    6.             dtaGridContacts.CurrentCell = New DataGridCell(hti.Row, hti.Column)
    7.             dtaGridContacts.Select(hti.Row)
    8.         End If
    9.  
    10.         'boxContactDetails.Text = "Details: " & dtaGridContacts(hti.Row, 1) & " " & dtaGridContacts(hti.Row, 2)
    11.         Dim dbCmd As OleDb.OleDbCommand
    12.         Dim strSQL = "SELECT * FROM Contacts WHERE contact_id='1'"
    13.  
    14.         dbCmd = New OleDb.OleDbCommand(strSQL, dbConn)
    15.         dbConn.Open()
    16.         Dim dbRead As OleDb.OleDbDataReader = dbCmd.ExecuteReader
    17.  
    18.         If dbRead.Read = True Then
    19.         boxContactDetails.Text = "Details: Of Contact"
    20.         Else
    21.            MessageBox.Show("The contact details you requested could not be " & vbCrLf & "displayed. Please hit the refresh button and try again...", "Contact does not exist!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
    22.         End If
    23.  
    24.         dbConn.Close()
    25.  
    26.     End Sub

    i have kept getting this error when i click the grid...this is the error message...
    Code:
    An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll
    and this is the line that i get the error on...
    VB Code:
    1. Dim dbRead As OleDb.OleDbDataReader = dbCmd.ExecuteReader

    Thanks, Justin

  2. #2

    Thread Starter
    Registered User
    Join Date
    Nov 2005
    Posts
    206

    Re: datagrid and datareader problem...

    would i be correct in assuming it would be the non prefered and slow way to add the entire "Contacts" table to the datagrid, and use the above methods, to systematically load the data into the text files...that would be the slow way...correct..

    the prefered way would be to load only 4 columns into the datagrid, rather than the 10-15 columns total, and then use an sql statement to select only one row, rather than load all 15 columns for like 100 or more rows??

    or is the difference negligable??

    Thanks, Justin

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: datagrid and datareader problem...

    Put your code in a Try...Catch block and catch the OledbException, check it's Message property.

  4. #4

    Thread Starter
    Registered User
    Join Date
    Nov 2005
    Posts
    206

    Re: datagrid and datareader problem...

    Quote Originally Posted by mendhak
    Put your code in a Try...Catch block and catch the OledbException, check it's Message property.
    Ok, so i put it into a try...catch block, and the ex.Message Returned This:
    Code:
    Data Type MissMatch in Criteria Expression
    thanks, Justin

    EDIT: Update!

    here is my new block of code...

    i changed
    VB Code:
    1. Dim strSQL = "SELECT * FROM Contacts WHERE contact_id='1'"
    to this:
    VB Code:
    1. Dim strSQL = "SELECT TOP 1 * FROM Contacts

    and the code ran fine...so its got to do with the where cause...so is that the correct syntax for the where contact_id = ?? using oledb.oledbCommand

    EDIT!!!:: RESOLVED.. i removed the single quotes arround the 1..worked fine.

    Thanks, Justin
    Last edited by freefall; Nov 25th, 2005 at 12:47 AM.

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