Reputation: 61
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?
Upvotes: 2
Views: 10137
Reputation: 28403
Try like this
If read.Read Then
If txt_jkpfind.Text.ToString <> read("OrderNoID").ToString() Then
jkpfindorderno = Val(txt_jkpfind.Text)
Me.Hide()
frm_Ecustjkpbookingsummary.Show()
End If
ElseIf read2.Read Then
If txt_jkpfind.Text.ToString <> read("OrderNoID").ToString() 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
Upvotes: 0
Reputation: 914
Having two connections is unnecessary. You're using a mdf database file. Which is an access database file. I'm pretty certain Access only allows one connection at a time. So you're first query might be blocking the second one. See if it works by just creating one connection.
Also, all your database connections are local to this function and have nothing to do with your data bindings. You will have to show details about how you have your binding setup for us to point out what might be wrong with it.
Also, you should use paramaterized queries to prevent sql injection and other things. To parameterize the first query do this.
Dim statement As String = "SELECT * FROM [JKPOrders] WHERE OrderNoID=@OrderId;"
Dim com As SqlCommand = New SqlCommand(statement, dbconnection)
com.Parameters.Add(new SqlParameter("@OrderId", txt_jkpfind.Text))
Upvotes: 1