[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:
Private Sub dtaGridContacts_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtaGridContacts.MouseUp
Dim rowPoint = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = dtaGridContacts.HitTest(rowPoint)
If hti.Type = DataGrid.HitTestType.Cell Then
dtaGridContacts.CurrentCell = New DataGridCell(hti.Row, hti.Column)
dtaGridContacts.Select(hti.Row)
End If
'boxContactDetails.Text = "Details: " & dtaGridContacts(hti.Row, 1) & " " & dtaGridContacts(hti.Row, 2)
Dim dbCmd As OleDb.OleDbCommand
Dim strSQL = "SELECT * FROM Contacts WHERE contact_id='1'"
dbCmd = New OleDb.OleDbCommand(strSQL, dbConn)
dbConn.Open()
Dim dbRead As OleDb.OleDbDataReader = dbCmd.ExecuteReader
If dbRead.Read = True Then
boxContactDetails.Text = "Details: Of Contact"
Else
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)
End If
dbConn.Close()
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:
Dim dbRead As OleDb.OleDbDataReader = dbCmd.ExecuteReader
Thanks, Justin
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
Re: datagrid and datareader problem...
Put your code in a Try...Catch block and catch the OledbException, check it's Message property.
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:
Dim strSQL = "SELECT * FROM Contacts WHERE contact_id='1'"
to this:
VB Code:
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