Reputation: 175
I am using a VB program connected to an access database. In my code, I want to check if a record exists at the specified row, and if not, do something. Checking if the row exists is my issue. In pseudocode, this is what I want to achieve:
If RecordAtLocationExists = False Then
...
End If
Code I have tried includes:
If DBDataSet.Tables(TableName).Rows(QuestionNumber).IsNull = True Then
If DBDataSet.Tables(TableName).Rows(QuestionNumber) = "" Then
If DBDataSet.Tables(TableName).Rows(QuestionNumber) = Nothing Then
If DBDataSet.Tables(TableName).Rows(QuestionNumber) = Null Then
None of the above code works. I have tried to search for a solution, but everything else seems far too complicated for this. I am probably approaching this wrong, but hopefully it makes sense what I am trying to achieve.
Any ideas?
Upvotes: 0
Views: 2389
Reputation: 4512
First of all, you are trying to check if the record exists at a DataTable
, not at database. Database data could have changed since you filled your DataTable
. Try to query the database directly:
Dim specificRow as Integer = 23 '<-- Set this variable to the specific row you are looking for
Dim query As String = "SELECT * FROM TableName WHERE QuestionNumber = ?"
Dim cmd As OleDbCommand = New OleDbCommand(query, myConnection)
cmd.Parameters.AddWithValue("questionnumber", specificRow)
dr = cmd.ExecuteReader
And test if the command return rows
If dr.Read() Then
' Do stuff here
Else
' Do another stuff here
End if
Upvotes: 2