I have created a SQL Database with a VB application. I have got a form that allows users to insert new records. Each new record has a unique ID number. I want the user to be able to search for a record using the ID number.
I have got one page with a text box that allows them to insert their ID number. Then they click submit. So the database is queried; if the ID number does not exist they get a message box with an error. If the ID number does exist a new page is displayed and the record is displayed.
I assume the database connection would begin on the first page when the user inputs their ID number. I have got two tables for orders so I need to query both tables. This is the code I have for the first page.
Try
Dim dbconnection As SqlConnection = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Cara\Documents\Visual Studio 2012\Projects\Online Portal Solutions\Online Portal Solutions\Online Portal Solutions Database.mdf;Integrated Security=True")
dbconnection.Open()
Dim statement As String = "SELECT * FROM [JKPOrders] WHERE OrderNoID='" & txt_jkpfind.Text & "';"
Dim com As SqlCommand = New SqlCommand(statement, dbconnection)
Dim read As SqlDataReader = com.ExecuteReader
Dim dbconnection2 As SqlConnection = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Cara\Documents\Visual Studio 2012\Projects\Online Portal Solutions\Online Portal Solutions\Online Portal Solutions Database.mdf;Integrated Security=True")
dbconnection2.Open()
Dim statement2 As String = "SELECT * FROM [ClarkeOrders] WHERE OrderNoID='" & txt_jkpfind.Text & "';"
Dim com2 As SqlCommand = New SqlCommand(statement2, dbconnection2)
Dim read2 As SqlDataReader = com2.ExecuteReader
If read.Read Then
If txt_jkpfind.Text.ToString <> read("OrderNoID") Then
jkpfindorderno = Val(txt_jkpfind.Text)
Me.Hide()
frm_Ecustjkpbookingsummary.Show()
End If
ElseIf read2.Read Then
If txt_jkpfind.Text.ToString <> read("OrderNoID") Then
jkpfindorderno = Val(txt_jkpfind.Text)
Me.Hide()
frm_Ecustjkpbookingsummary.Show()
End If
Else
MessageBox.Show("no.", "No Entry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
End Try
It identifies when a record does not exist and displays the message box but when the right ID number is input it doesn't do anything, what am I doing wrong?
Also, I have got a data adapter, data binder, etc on the following page that displays the record. Is this how I display the record and how do I do that?