Error while connection to database and filling source dataset - sql

so I'm getting this error:
The main thing is that sometimes it might run smooth and get no errors some others i get the errors that what i don't get. Also, SQL wise the code is running there is no problem with that. I don't know what might be wrong any ideas would be helpful thanks in advance. Also the line I get the error is Adapter.Fill(SourceDataSet).
Try
'Δήλωση Μεταβλητών
strConnection = String.Format("Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};User ID={2};Password={3};",
strServer, strDataBase, strUserName, strPassword)
' Create a connection object.
Dim Connection As New OleDbConnection(strConnection)
' Create a data adapter.
Dim Adapter As New OleDbDataAdapter("USE EMBONILO_B
SELECT M.CODE AS ΚΩΔΙΚΟΣ_ΕΙΔΟΥΣ , M.DESCRIPTION AS ΠΕΡΙΓΡΑΦΗ_ΕΙΔΟΥΣ , M.SUBCODE1 AS ΚΩΔΙΚΟΣ_ΚΑΤΑΣKEYAΣΤΗ , MF.DESCR AS BRAND , TS.DESCR AS ΣΑΙΖΟΝ, IG.DESCR AS ΟΜΑΔΑ_ΕΙΔΟΥΣ , TF.DESCR AS ΦΥΛΟ , IG2.DESCR AS ΤΥΠΟΣ ,
SC.DESCR AS SPORTSCODE , M.SUBCODE2 AS ΠΕΡΙΓΡ_ΧΡΩΜΑΤΟΣ , M.COMPOSITION AS ΣΥΝΘΕΣΗ , MS.DESCR AS ΜΟΝ_ΜΕΤΡ , VAT.VATDESCR AS ΦΠΑ , SL.DESCR AS ΜΕΓΕΘΟΛΟΓΙΟ , IC.COLORDESCR AS ΚΩΔ_ΧΡΩΜ ,
M.DESCR2 AS ΕΝΑΛ_ΠΕΡΙΓΡ , M.WHSPRICE AS ΧΟΝΔΡΙΚΗ , M.RTLPRICE AS ΛΙΑΝΙΚΗ , M.FLDSTRING1 AS ΠΧ_ΦΥΛΟ , M.FLDSTRING2 AS ΠΧ_ORIGIN_COUNTRY , M.FLDSTRING3 AS ΠΧ_ΣΑΙΖΟΝ , M.FLDSTRING5 AS ΠΧ_ΕΤΟΣ ,
M.FLDSTRING4 AS ΠΧ_ΚΑΤΗΓΟΡΙΑ , M.FLDSTRING6 AS ΠΧ_ΧΡΩΜΑ
FROM MATERIAL M
INNER JOIN MANUFACTURER MF ON MF.CODEID = M.MNFID
INNER JOIN ITEMGROUP IG ON IG.CODEID = M.IGPID
INNER JOIN IP_TBLFILLO TF ON TF.CODEID=M.IP_EPS_MODEL
INNER JOIN ITEMGROUP2 IG2 ON IG2.CODEID=M.IGSID
INNER JOIN INF_SPORTSCODE SC ON SC.CODEID=M.INF_SPORTSCODE
INNER JOIN MESUNIT MS ON MS.CODEID=M.MU1
INNER JOIN VATCATEGORY VAT ON VAT.CODEID=M.VTCID
INNER JOIN SIZELIST SL ON SL.CODEID=M.MAINSZLID
INNER JOIN ITECOLOR IC ON IC.ITEID=M.ID
INNER JOIN ITEMCATEGORY TS ON TS.CODEID=M.ICTID
WHERE M.CODE LIKE '" & txtSearch.Text & "%'", Connection)
' Create and fill a dataset.
Dim SourceDataSet As New DataSet()
Adapter.Fill(SourceDataSet)
' Specify the data source for the grid control.
grdMain.DataSource = SourceDataSet.Tables(0)
If GridView2.RowCount = 0 Then
MsgBox("Δεν υπάρχει τέτοιος κωδικός.", vbOKOnly)
grdMain.DataSource = Nothing
End If
Connection.Close()
Catch oleDbExceptionParameter As OleDbException
DevExpress.XtraEditors.XtraMessageBox.Show(oleDbExceptionParameter.StackTrace.ToString, Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, My.Application.Info.Title)
End Try

Related

I have A problem with dividing the value of 0 by 0 in an Access database

When I try to extract the result of Division 2 Field from table in access database
If I have a value of 0 an error occurs
sqlSTR = "SELECT TBL_Category_Item_File.Item_Org_Price2/TBL_Stocks_Balances.Item_QTY AS ['Price']FROM (((TBL_Category_Item_File INNER JOIN TBL_Suppliers_Product ON TBL_Category_Item_File.Item_ID = TBL_Suppliers_Product.Item_ID) INNER JOIN TBL_Suppliers ON TBL_Suppliers_Product.Supp_ID = TBL_Suppliers.Supp_ID) INNER JOIN TBL_Sub_categories ON TBL_Category_Item_File.ID_Sub_categories = TBL_Sub_categories.ID_Sub_categories) INNER JOIN TBL_Stocks_Balances ON (TBL_Stocks_Balances.Item_ID = TBL_Category_Item_File.Item_ID) AND (TBL_Category_Item_File.Item_BarCode = TBL_Stocks_Balances.Item_Barcode) WHERE tbl_Category_Item_File.Catg_ID =" & Split(cmblist.Text, " - ")(0)
If you are trying to avoid the error, you can use a CASE statement in your query to check for a 0 value in the Item_Org_Price2 field and return a different value if it is 0. For example, you could do something like this:
SELECT
CASE
WHEN TBL_Category_Item_File.Item_Org_Price2 = 0
THEN 0
ELSE TBL_Category_Item_File.Item_Org_Price2/TBL_Stocks_Balances.Item_QTY
END AS ['Price']
FROM (((TBL_Category_Item_File INNER JOIN TBL_Suppliers_Product ON TBL_Category_Item_File.Item_ID = TBL_Suppliers_Product.Item_ID) INNER JOIN TBL_Suppliers ON TBL_Suppliers_Product.Supp_ID = TBL_Suppliers.Supp_ID) INNER JOIN TBL_Sub_categories ON TBL_Category_Item_File.ID_Sub_categories = TBL_Sub_categories.ID_Sub_categories) INNER JOIN TBL_Stocks_Balances ON (TBL_Stocks_Balances.Item_ID = TBL_Category_Item_File.Item_ID) AND (TBL_Category_Item_File.Item_BarCode = TBL_Stocks_Balances.Item_Barcode) WHERE tbl_Category_Item_File.Catg_ID =" & Split(cmblist.Text, " - ")(0)

How to create RDLC report with joined tables?

I'm trying to create a report that will display my table that has joins from different tables that looks likes this,
select * from tblproduct As p inner join tblbrand As b On p.bid = b.brandid inner Join tblclassification As c On p.cid = c.classid inner Join tblformulation As f On p.fid = f.formid inner Join tblgeneric As g On p.gid = g.genericid inner Join tbltype As t On p.tid = t.typeid
My table consist of 8 columns and 4 of it is column id's for joins, I can succesfully use the query when im trying to display to datagridview but when I try this in reportviewer it doesn't work and gives me error: "Object reference not sent to an instance of an object". This is what I tried,
Public Class frmInventoryReport
Public Property strReport2 As String
Private Sub frmInventoryReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.tblproductTableAdapter.Fill(Me.groceryDataSet.tblproduct)
Me.ReportViewer1.RefreshReport()
Dim rptDataSource As New ReportDataSource
Try
With Me.ReportViewer1.LocalReport
.ReportPath = "D:\VisualBasic\EELPOS - Copy\EELPOS\Report3.rdlc"
.DataSources.Clear()
End With
Select Case strReport2
Case "Report3"
Dim i As Integer = 0
Dim sqlQuery As String = "select * from tblproduct As p inner join tblbrand As b On p.bid = b.brandid inner Join tblclassification As c On p.cid = c.classid inner Join tblformulation As f On p.fid = f.formid inner Join tblgeneric As g On p.gid = g.genericid inner Join tbltype As t On p.tid = t.typeid"
Dim da As New MySqlDataAdapter(sqlQuery, cn)
Dim dt As DataTable = New DataTable()
While dr.Read
dt.Rows.Add(i, dr.Item("barcode").ToString, dr.Item("generic").ToString, dr.Item("classification").ToString, dr.Item("type").ToString, dr.Item("formulation").ToString, dr.Item("price").ToString, dr.Item("qty").ToString)
End While
da.Fill(dt)
rptDataSource = New ReportDataSource("inventory", dt)
End Select
Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Me.ReportViewer1.RefreshReport()
End Sub
End Class
What's the correct way to do it?

how to use an in clause to check if a column is a certain string or not when referencing 3 tables

I have this SQL statement and it joining 2 tables...
SELECT TRIDENT.Maintenance.RCFAs.*, TRIDENT.Maintenance.Equipment.EquipmentName
FROM TRIDENT.Maintenance.RCFAs
LEFT JOIN TRIDENT.Maintenance.Equipment
ON TRIDENT.Maintenance.Equipment.EquipmentId = TRIDENT.Maintenance.RCFAs.EquipmentId
WHERE 1 = 1
Now I have to reference a third table because I have a dropdown by the name of components that we will use to lookup stuff on the table named TRIDENT.Maintenance.RCFAXrefComponents. So if "bolts" is selected from the dropdown it will look up the YEAR and RCFAID which makes a unqiue key for both RCFAXrefComponents and RCFAs and checks if there are any rows in RCFAs that had "bolts" in the component column when looking up that YEAR and RCFAId. A coworker suggested I use an IN clause to check if the YEAR and RCFAId is in that RCFAXrefComponents. He said it might be tricky, but I'm a little lost on how to do this.
Below is the RCFAs table that I want to grab the YEAR and RCFAId values together and lookup on the other to see if that bolt is also there. Then it should bring up the line item on RCFAs table to output to user
this is RCFAXrefComponents table below
Public Shared Function GetRCFAList(rcfaNumber As Integer?, description As String,
failureType As String, equipmentDescription As String, componentSelection As String) As List(Of RCFA)
Dim sql = "SELECT r.*, e.EquipmentName FROM TRIDENT.Maintenance.RCFAs AS r LEFT JOIN TRIDENT.Maintenance.Equipment AS e ON e.EquipmentId = r.EquipmentId JOIN TRIDENT. Maintenance.RCFAXrefComponents AS c ON c.Year = r.Year AND c.RCFAId = r.RCFAId WHERE"
If failureType <> "" Then
sql += " AND RCFAs.FailureType = '" & failureType.ToUpper & "'"
End If
If description <> "" Then
sql += " AND RCFAs.ShortDesc LIKE '%" & description.ToUpper & "%'"
End If
If equipmentDescription <> "" Then
sql += " AND Equipment.EquipmentName LIKE '%" & equipmentDescription.ToUpper & "%'"
End If
If componentSelection <> "" Then
sql += " c.ComponentId = '%" & componentSelection.ToUpper & "%'"
End If
Dim dbConn As New Trident.Core.DBConnection
Dim ds = dbConn.FillDataSet(sql)
Dim tmpList As New List(Of RCFA)
For Each dr As DataRow In ds.Tables(0).Rows
tmpList.Add(New RCFA(New Trident.Objects.Maintenance.RCFA.RCFA(dr)))
Next
Return tmpList
End Function
Just use a simple JOIN with the RFCAXrefComponents table, and a WHERE clause that filters the component ID.
SELECT r.*, e.EquipmentName
FROM TRIDENT.Maintenance.RCFAs AS r
LEFT JOIN TRIDENT.Maintenance.Equipment AS e
ON e.EquipmentId = r.EquipmentId
JOIN TRIDENT.Maintenance.RCFAXrefComponents AS c
ON c.Year = r.Year
AND c.RCFAId = r.RCFAId
WHERE c.ComponentID = 'BOLTS'
I may be confused but it sounds like you need to use EXISTS.
SELECT r.*,
e.EquipmentName
FROM TRIDENT.Maintenance.RCFAs r
LEFT JOIN TRIDENT.Maintenance.Equipment e ON e.EquipmentId = r.EquipmentId
WHERE EXISTS ( SELECT 1
FROM TRIDENT.Maintenance.RCFAXrefComponents c
WHERE c.Year = r.Year
AND c.RCFAId = r.RCFAId
AND c.ComponentId LIKE '%BOLTS%' )
you may not need the c.ComponentId LIKE '%BOLTS%' you might just want c.ComponentId = 'BOLTS'

Paradox DB SQL Multiple JOINS

I'm working on a legacy VB6 project and I need to make a JOIN call like this:
SELECT C.Cnum, C.RealDate, M.Name, R.Price, R.Qnt, R.RealPrice, R.QntP, R.QntR, M.Name
FROM "CHECK" C
LEFT JOIN "RCHECK" R ON C.Cnum = R.Cnum
LEFT JOIN "PCHECK" P ON C.Cnum = P.Cnum
LEFT JOIN "MONEY" M ON P.Curency = M.Sifr
LEFT JOIN "MENU" MN ON R.Sifr = MN.Sifr
WHERE C.Cnum > 0 ORDER BY C.Cnum
I use "Driver={Microsoft Paradox Driver (*.db )};DriverID=538" as a part of connection string but it seems it doesn't support more than one join! Which is weird.
Any ideas how to solve/workaround it?
And yes, when I run this query in Borland Database Desktop, it works fine.
Update 1:
My VB Code:
Dim Conn As New ADODB.Connection
Dim sConnStr As String
Dim sQuery As String
sConnStr = "Driver={Microsoft Paradox Driver (*.db )};DriverID=538;Fil=Paradox 5.X;CollatingSequence=ASCII;DBQ=C:\DBTOFTP\BUFF;DefaultDir=C:\DBTOFTP\BUFF;PWD=SOMEPASS;"
sQuery = "SELECT C.Cnum, C.RealDate, M.Name, R.Price, R.Qnt, R.RealPrice, R.QntP, R.QntR, M.Name " & _
"FROM ""CHECK"" C " & _
"LEFT JOIN ""RCHECK"" R ON C.Cnum = R.Cnum " & _
"LEFT JOIN ""PCHECK"" P ON C.Cnum = P.Cnum " & _
"LEFT JOIN ""MONEY"" M ON P.Curency = M.Sifr " & _
"LEFT JOIN ""MENU"" MN ON R.Sifr = MN.Sifr " & _
"WHERE C.Cnum > 0 " & _
"ORDER BY C.Cnum"
Conn.ConnectionString = sConnStr
Conn.Open
Some old drivers often requires that multiple JOINs must be enclosed in parentheses.
Try something like this:
FROM
(
"CHECK" C
INNER JOIN
"RCHECK" R
ON C.Cnum = R.Cnum
)
INNER JOIN
"PCHECK" P
ON P.Cnum = C.Cnum

Syntax error (missing operator) in query expression

I have the following SQL Statement that works fine in SQL Server but in Access gets me an error
'ERM_HAZARD.[HAZARDID] = ERM_XREF_HAZARD_CAT.[HAZARDID]
INNER JOIN ERM_HAZARDSTATUS
ON ERM_HAZARD.HAZARDSTATUSID = ERM_HAZARDSTATUS.HAZARDSTATUSI'
SELECT erm_hazard.hazardid AS PKTrans,
erm_hazard.extref AS No,
erm_cat.name AS CaseType,
erm_hazard.created AS CaseDate,
erm_hazard.name AS Title,
erm_hazard.description AS DescriptionTrans,
erm_organisation.name AS ResponsibleUnit,
erm_hazard.reviewdate AS CaseDeadline,
erm_hazardstatus.name AS Status,
erm_organisation.name AS UnitInCharge,
erm_personel.name AS PersonInCharge
FROM (erm_personel
INNER JOIN (erm_organisation
INNER JOIN erm_hazard
ON erm_organisation.[organisationid] =
erm_hazard.[organisationid])
ON erm_personel.[personelid] = erm_hazard.[personelid])
INNER JOIN (erm_cat
INNER JOIN erm_xref_hazard_cat
ON erm_cat.[catid] = erm_xref_hazard_cat.[catid])
ON erm_hazard.[hazardid] = erm_xref_hazard_cat.[hazardid]
INNER JOIN erm_hazardstatus
ON erm_hazard.hazardstatusid = erm_hazardstatus.hazardstatusid
WHERE (( ( erm_xref_hazard_cat.periodid ) = (SELECT Max(
erm_xref_hazard_cat.periodid)
FROM erm_xref_hazard_cat) ))
ORDER BY erm_xref_hazard_cat.periodid DESC,
erm_hazard.hazardid,
erm_cat.name
Is this a Access SQL Size problem?
Any ideas?
MS Access is very particular with the parentheses in queries. When you have multi-table joins a pair of parentheses must be around each join:
SELECT ERM_HAZARD.HAZARDID AS PKTrans,
ERM_HAZARD.EXTREF AS No,
ERM_CAT.NAME AS CaseType,
ERM_HAZARD.CREATED as CaseDate,
ERM_HAZARD.NAME AS Title,
ERM_HAZARD.DESCRIPTION AS DescriptionTrans,
ERM_ORGANISATION.NAME AS ResponsibleUnit,
ERM_HAZARD.REVIEWDATE AS CaseDeadline,
ERM_HAZARDSTATUS.NAME as Status,
ERM_ORGANISATION.NAME AS UnitInCharge,
ERM_PERSONEL.NAME AS PersonInCharge
FROM ((((ERM_PERSONEL
INNER JOIN ERM_HAZARD
ON ERM_PERSONEL.[PERSONELID] = ERM_HAZARD.[PERSONELID])
INNER JOIN ERM_ORGANISATION
ON ERM_ORGANISATION.[ORGANISATIONID] = ERM_HAZARD.[ORGANISATIONID])
INNER JOIN ERM_XREF_HAZARD_CAT
ON ERM_HAZARD.[HAZARDID] = ERM_XREF_HAZARD_CAT.[HAZARDID])
INNER JOIN ERM_CAT
ON ERM_CAT.[CATID] = ERM_XREF_HAZARD_CAT.[CATID])
INNER JOIN ERM_HAZARDSTATUS
ON ERM_HAZARD.HAZARDSTATUSID = ERM_HAZARDSTATUS.HAZARDSTATUSID
WHERE ERM_XREF_HAZARD_CAT.PERIODID= (SELECT MAX(ERM_XREF_HAZARD_CAT.PERIODID)
FROM ERM_XREF_HAZARD_CAT)
ORDER BY ERM_XREF_HAZARD_CAT.PERIODID DESC, ERM_HAZARD.HAZARDID, ERM_CAT.NAME
To add to this. If you are creating a Pass-Through Query, you need to identify the connections string before declaring the SQL.
sub test()
Dim dbs As Database
Dim qdf As DAO.QueryDef
Dim rstSQL As DAO.Recordset
Dim strSQL As String
On Error GoTo errTrap
Set dbs = CurrentDb
Set qdf = dbs.CreateQueryDef("")
qdf.Connect = ConStringMailing
qdf.ReturnsRecords = True
qdf.SQL = "Select * FROM TABLE"
Set rstSQL = qdf.OpenRecordset
errTrap:
end sub
'ERM_HAZARD.[HAZARDID] = ERM_XREF_HAZARD_CAT.[HAZARDID]
INNER JOIN ERM_HAZARDSTATUS
ON ERM_HAZARD.HAZARDSTATUSID = ERM_HAZARDSTATUS.HAZARDSTATUSI'
Missing de "D" at the end of HAZARDSTATUSID