Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Database Help

Status
Not open for further replies.

tempoman

Programmer
May 9, 2000
41
AU
Could someone please look over this code and see what I am doing wrong? I can successfully connect to the database however if I true to Select the record that matches the Surname a user enters from the database it returns an error:
----------------------------
"An unhandled exception of type 'System.InvalidOperationException' occurred in microsoft.data.odbc.dll

Additional information: No data exists for the row/column.
-----------------------------

Here is my code:

----------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cn As OdbcConnection
cn = New OdbcConnection("Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=c:\Mouse Tails\Database\prototype1.mdb;UID=;PWD=")

cn.Open()
Dim mystring As String = "Select * from Owner where OwnerSname Like 'UserName.Text'"
Dim cmd As OdbcCommand = New OdbcCommand(mystring, cn)
Dim myReader As OdbcDataReader
Dim Fname As Object
myReader = cmd.ExecuteReader()

If myReader(&quot;OwnerFname&quot;) <> 0 Then
Password.Text = myReader(&quot;OwnerFname&quot;)
End If


cn.Close()
End Sub

-----------------------------------------------------
 
Your code:
Code:
Dim mystring As String = &quot;Select * from Owner where OwnerSname Like 'UserName.Text'&quot;
Results in UserName.Text coming through as literal text, and not the contents of the textbox like you expect. I'm used to C#, not VB.NET, so my code might look a little weird, but try this:
Code:
Dim MyString As StringBuilder = &quot;&quot;
MyString.Append(&quot;SELECT * FROM Owner WHERE OwnerSname LIKE '&quot;)
MyString.Append(UserName.Text)
MyString.Append(&quot;'&quot;)

Dim cmd As OdbcCommand = New OdbcCommand(MyString.ToString(), cn)
Something else you might want to do is surround this code in a try..catch..finally block, so that if the ODBC layer raises an exception you'll be able to catch it. You'll need to make some code changes regarding variable declaration for that, though.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top