I'm working with 2 different DB, one is an SQL server and the other is a MySQL.
I'm trying to cross the data between them. The 2 DB have one reference of product in common. But both use very different data.
For example in Mysql I have simple data like, description, price, type .... And in SQL I have Last price used, discounts, barcode .... But there are some products that don't have data in both DB, so they have to remain empty in some fields. And I Have to cross this data into a ListView.
I have this code so far:
Private Sub button_search_Click(sender As Object, e As EventArgs) Handles button_search.Click
'--------------------------------MySQL
Call connect()
Dim marca_prod As String
Dim fam_prod As String
Dim tipo_prod As String
Dim sql As String
rs = New ADODB.Recordset
If type_1.Checked Then
tipo_prod = "1"
ElseIf type_2.Checked Then
tipo_prod = "2"
Else
tipo_prod = ""
End If
If proc_desc.Checked Then
sql = "SELECT p.referencia AS Referencia, p.descricao AS Descrição, p.tipo AS Tipo, f.nome_familia AS Familia, m.nome_marca AS Marca FROM ((produtos AS p INNER JOIN familias AS f ON p.Id_familia = f.Id_familia) INNER JOIN marcas AS m ON p.Id_marca = m.Id_marca) WHERE p.descricao LIKE '%" & Prod_input.Text & "%' AND p.tipo LIKE '" & tipo_prod & "%' "
ElseIf proc_ref.Checked Then
sql = "SELECT p.referencia AS Referencia, p.descricao AS Descrição, p.tipo AS Tipo, f.nome_familia AS Familia, m.nome_marca AS Marca FROM ((produtos AS p INNER JOIN familias AS f ON p.Id_familia = f.Id_familia) INNER JOIN marcas AS m ON p.Id_marca = m.Id_marca) WHERE p.referencia LIKE '%" & Prod_input.Text & "%' AND p.tipo LIKE '" & tipo_prod & "%' "
Else
sql = "SELECT p.referencia AS Referencia, p.descricao AS Descrição, p.tipo AS Tipo, f.nome_familia AS Familia, m.nome_marca AS Marca FROM ((produtos AS p INNER JOIN familias AS f ON p.Id_familia = f.Id_familia) INNER JOIN marcas AS m ON p.Id_marca = m.Id_marca) WHERE (p.referencia LIKE '%" & Prod_input.Text & "%' OR p.descricao LIKE '%" & Prod_input.Text & "%') AND p.tipo LIKE '" & tipo_prod & "%' "
End If
rs.Open(sql, conn)
query_results.Items.Clear()
While Not rs.EOF
Dim lv As New ListViewItem
lv = query_results.Items.Add(rs.Fields(0).Value)
lv.SubItems.Add(rs.Fields(1).Value)
lv.SubItems.Add(rs.Fields(2).Value)
lv.SubItems.Add(rs.Fields(3).Value)
lv.SubItems.Add(rs.Fields(4).Value)
rs.MoveNext()
End While
'-----------------------------SQL
Dim myDBCnn As SqlConnection
Dim myDBCmd As SqlCommand
Dim myDBReader As SqlDataReader
myDBCnn = New SqlConnection("************")
myDBCnn.Open()
Dim MystrQ As String
Dim i = 0
While Not rs.EOF
MystrQ = "Select stock, epcult, epcpond FROM st WHERE ref ='" & rs.Fields(0).Value & "'"
myDBCmd = New SqlCommand(MystrQ, myDBCnn)
myDBReader = myDBCmd.ExecuteReader
While myDBReader.Read
Dim lv As New ListViewItem
lv = query_results.Items.Add(myDBReader.Item(5))
lv.SubItems.Add(myDBReader.Item(6))
lv.SubItems.Add(myDBReader.Item(7))
End While
End While
myDBReader.Close()
myDBCmd.Dispose()
myDBCnn.Close()
If query_results.Items.Count = 0 Then
MsgBox("Não foram encontrados registos para os parametros selecionados!")
End If
count_label.Text = query_results.Items.Count
End Sub
At the moment I got an error in the myDBReader.Close() at the end -> "Object reference not set to an instance of an object" But i think the other code is not right. What can I do ?