vb.net - Index was outside the bounds of the array - vb.net

I got a problem in vb.net. By the way I am a newbie. I need your help
what's problem on this error:
My code:
Public Sub load_stockid_monthly_byUP(ByVal type As Integer, ByVal filter As Integer, ByVal input As String)
Dim sqlquery As String = ""
Dim myCommand As New MySqlCommand
Dim myData As MySqlDataReader
Dim x As Integer = 0
Dim where As String = ""
Dim order As String = ""
If filter = 1 Then
where = ""
ElseIf filter = 2 Then
where = " AND r.type =" & input
ElseIf filter = 3 Then
where = " AND s.cat_id =" & input
ElseIf filter = 4 Then
where = " AND (s.desp like '% " & input & " %' OR s.desp like '" & input & " %' OR s.desp like '% " & input & "' OR s.desp = '" & input & "')"
ElseIf filter = 5 Then
where = " AND (s.cat_id = 1 OR s.cat_id = 2 OR s.cat_id = 4 OR s.cat_id = 5 )"
ElseIf filter = 6 Then
where = " AND (s.pallet_id <> 0 OR s.carton_id <> 0)"
order = " ORDER BY carton_id, pallet_id , stock_id"
ElseIf filter = 7 Then
where = " AND (s.pallet_id <> 0)"
order = " ORDER BY pallet_id , stock_id"
ElseIf filter = 8 Then
where = " AND (s.carton_id <> 0)"
order = " ORDER BY carton_id, stock_id"
ElseIf filter = 9 Then
where = " AND (s.uniform_id <> 0)"
order = " ORDER BY uniform_id"
End If
If type = 1 Then
sqlquery = "(SELECT s.id AS id,s.stock_id AS stock_id ,s.packing_id AS packing_id ,s.carton_id AS carton_id, s.pallet_id AS pallet_id, s.desp AS desp,i.unit_price AS unit_price, i.rate AS rate, c.type AS cat, u.unit AS uom, s.currency AS currency, s.uniform_id As uniform_id FROM incoming AS i LEFT JOIN stock AS s ON i.stock_id = s.id LEFT JOIN uom AS u ON s.uom = u.id LEFT JOIN stock_cat AS c ON s.cat_id = c.id LEFT JOIN supplier AS r ON s.supplier_id = r.id WHERE s.active = 1 AND (i.active = 1 OR i.active = 2)" & where & " GROUP BY c.type, s.stock_id , i.unit_price, i.rate ) UNION " _
& "(SELECT s.id AS id,s.stock_id AS stock_id ,s.packing_id AS packing_id ,s.carton_id AS carton_id, s.pallet_id AS pallet_id,s.desp AS desp,i.unit_price AS unit_price, i.rate AS rate, c.type AS cat, u.unit AS uom, s.currency AS currency, s.uniform_id As uniform_id FROM adjust AS i LEFT JOIN stock AS s ON i.stock_id = s.id LEFT JOIN uom AS u ON s.uom = u.id LEFT JOIN stock_cat AS c ON s.cat_id = c.id LEFT JOIN supplier AS r ON s.supplier_id = r.id WHERE s.active = 1 AND i.adjust_qty > 0 AND (i.active = 1 OR i.active = 2)" & where & " GROUP BY c.type, s.stock_id , i.unit_price, i.rate ) " & order
' sqlquery = "SELECT s.id AS id,s.stock_id AS stock_id ,s.desp AS desp,i.unit_price AS unit_price, i.rate AS rate, c.type AS cat, u.unit AS uom, s.currency AS currency FROM incoming AS i LEFT JOIN stock AS s ON i.stock_id = s.id LEFT JOIN uom AS u ON s.uom = u.id LEFT JOIN stock_cat AS c ON s.cat_id = c.id LEFT JOIN supplier AS r ON s.supplier_id = r.id WHERE (i.active = 1 OR i.active = 2)" & where & " GROUP BY c.type, s.stock_id , i.unit_price ORDER BY c.type ,s.stock_id "
ElseIf type = 2 Then
sqlquery = "SELECT s.id AS id,s.stock_id AS stock_id ,s.desp AS desp,n.unit_price AS unit_price, n.rate AS rate, c.type AS cat, u.unit AS uom, s.currency AS currency FROM issue AS i LEFT JOIN stock AS s ON i.stock_id = s.id LEFT JOIN uom AS u ON s.uom = u.id LEFT JOIN incoming AS n ON i.irm_id = n.id LEFT JOIN stock_cat AS c ON s.cat_id = c.id LEFT JOIN supplier AS r ON s.supplier_id = r.id WHERE s.active = 1 AND i.active = 1" & where & " GROUP BY c.type, s.stock_id , n.unit_price ORDER BY c.type ,s.stock_id"
End If
stock_id_count = 0
ConnectmyDB()
myCommand.Connection = conn
myCommand.CommandText = sqlquery
myData = myCommand.ExecuteReader
If myData.HasRows = False Then
x = 1
Else
While myData.Read
data(x, 0) = myData.GetInt32("id")
data(x, 1) = myData.GetString("stock_id")
data(x, 2) = myData.GetString("desp")
data(x, 8) = myData.GetString("unit_price")
data(x, 9) = myData.GetString ("rate")
data(x, 12) = myData.GetString("cat")
data(x, 14) = myData.GetString("uom")
data(x, 16) = myData.GetString("packing_id")
x = x + 1
End While
End If
DisconnectDatabase()
stock_id_count = x
End Sub 'closing stock unit price
I get the error:
Index was outside the bounds of the array for that code 'data(x, 0) = myData.GetInt32("id")'
How to fix that?
This is what i get when run in program:
************** Exception Text **************
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Purchasing.mdlStoreBalance.load_stockid_monthly_byUP(Int32 type, Int32 filter, String input)
at Purchasing.frmStockBalanceReport.bt_preview_Click(Object sender, EventArgs e)
Thank you

Where is data(x,?) defined? You fill data within a while loop, but there is no checking to see if the while loop overruns the defined bounds.
I also can't tell how much data you expect to read. This will influence the run-time for the while loop and how high x is expected to get.
(would have asked this as a comment, but my rep is not yet high enough in this form).
As an aside, the checking of filter can be done in a select case statement. This will allow you to double check that you have covered where and order consistently. Also, as a coding convention, naming variables exactly the same as a one of your keywords (in this case a SQL keyword) can hide simple errors. Calling them something like whereStmnt and orderStmnt might help.

Related

Postgres Left Join ignores row if ManyToOne property is null

In Spring Boot i have the following query in a PostgreSQL database, to get data from ConfigElement table and set them to the new one ConfigurationReview table. It works properly as expected:
#Query(
"SELECT new xx.xx.xx.dao.ConfigurationReview(r.id, MAX(ce.id) AS configElementId,"
+ " r.checkMethod, r.targetDate, r.endDate, r.sessionDate, SUM(CASE WHEN i.criticality ="
+ " 0 AND i.relation = 0 THEN 1 ELSE 0 END) AS majorFindings, SUM(CASE WHEN i.criticality"
+ " = 1 AND i.relation = 0 THEN 1 ELSE 0 END) AS minorFindings, r.state) FROM"
+ " ConfigElement ce "
+ "LEFT JOIN ce.reviews r ON r.configElement.id = ce.id "
+ "LEFT JOIN r.criteria cr ON cr.review.id = r.id "
+ "LEFT JOIN cr.issues i ON i.criterion.id = cr.id "
+ "WHERE ce.id = :configurationId AND r.id IS NOT NULL GROUP BY r.id")
Page<ConfigurationReview> findConfigurationReviews(
#Param("configurationId") Long configurationId, Pageable pageable);
There was a need to add another property (r.milestone) with a ManyToOne relation, so i added it to the query:
#Query(
"SELECT new xx.xx.xx.dao.ConfigurationReview(r.id, MAX(ce.id) AS configElementId,"
+ " r.checkMethod, r.targetDate, r.endDate, r.sessionDate, SUM(CASE WHEN i.criticality ="
+ " 0 AND i.relation = 0 THEN 1 ELSE 0 END) AS majorFindings, SUM(CASE WHEN i.criticality"
+ " = 1 AND i.relation = 0 THEN 1 ELSE 0 END) AS minorFindings, r.state, r.milestone) FROM"
+ " ConfigElement ce "
+ "LEFT JOIN ce.reviews r ON r.configElement.id = ce.id "
+ "LEFT JOIN r.criteria cr ON cr.review.id = r.id "
+ "LEFT JOIN cr.issues i ON i.criterion.id = cr.id "
+ "WHERE ce.id = :configurationId AND r.id IS NOT NULL GROUP BY r.id")
Page<ConfigurationReview> findConfigurationReviews(
#Param("configurationId") Long configurationId, Pageable pageable);
Here is the milestone entry in ConfigurationReview dao:
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(foreignKey = #ForeignKey(name = "fk_review_milestones_on_milestone_id"))
private Milestone milestone;
With the above changes, it works properly if r.milestone is not null. But when the r.milestone is null, the complete row is ignored in left join.
As a result, if I have 2 r.milestones with data and 2 r.milestones without, the above query returns only two entries instead of four.
The problem appears only if r.milestone is null (because of the ManyToOne relation?). If another property is null, the row is returned properly.
Any idea how can I fix this?

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)

SQL query for select statement when certain bind parameters were entered

I need help with a select statement. I want to use this for my search API where if you pass in certain parameters, fist Name, or last Name, or both, or first Name and state, the query will run as other parameters were not filled it.
Is there a way to do that? This is my query
select d.is_purged, d.is_reorg, ds.dlr_nm, ds.city, c.first_nm, c.middle_nm,
c.last_nm, c.is_mdd, ds.state, lds.display_name, c.is_wrn, d.crt_ts, d.upd_ts
from deal d
left join candidate c on d.candidate_id = c.id
left join lkup_deal_status lds on d.status = lds.status
left join dealership ds on d.id = ds.deal_id
where (c.first_nm is null or c.first_nm = :firstName )
and (d.is_purged = false )
and (ds.dlr_nm is null or ds.dlr_nm = :dealershipName)
and (ds.city is null or ds.city = :city)
and (c.middle_nm is null or c.middle_nm = :middleName)
and (c.last_nm is null or c.last_nm = :lastName)
and (ds.state is null or ds.state = :state)
and (lds.display_name is null or lds.display_name = :status)
thanks in advance
I ended up figuing it out.
For Java: String SEARCH_DEAL = "select d.id, ds.id, ds.dlr_nm, d.bac,
d.is_reorg, c
c.first_nm, c.middle_nm, " +
"c.last_nm, c.is_mdd, c.is_wrn, ds.city, ds.state, ds.rgn,
lds.display_name, d.crt_ts, d.upd_ts " +
"from mhdid_171749.deal d left join candidate c on d.candidate_id = c.id
" +
"left join lkup_deal_status lds on d.status = lds.status " +
"left join dealership ds on d.id = ds.deal_id " +
"where d.is_purged = false " +
" and (:dealershipName::varchar is null or ds.dlr_nm = :dealershipName)"
+
" and (:firstName::varchar is null or c.first_nm = :firstName ) " +
" and (:lastName::varchar is null or c.last_nm = :lastName) " +
" and (:bacCode::int is null or d.bac = :bacCode)" +
" and (:city::varchar is null or ds.city = :city) " +
" and (:state::varchar is null or ds.state = :state) " +
" and (:region::varchar is null or ds.rgn = :region) " +
" and (:status::varchar is null or lds.display_name = :status) ";
For SQL:
select d.is_purged, d.is_reorg, ds.dlr_nm, ds.city, c.first_nm,
c.middle_nm,
c.last_nm, c.is_mdd, ds.state, lds.display_name, c.is_wrn, d.crt_ts,
d.upd_ts
from mhdid_171749.deal d
left join candidate c on d.candidate_id = c.id
left join lkup_deal_status lds on d.status = lds.status
left join dealership ds on d.id = ds.deal_id
where d.is_purged = false
and (:firstName is null or c.first_nm = :firstName )
and (:dealershipName is null or ds.dlr_nm = :dealershipName)
and (:city is null or ds.city = :city)
and (:middleName is null or c.middle_nm = :middleName)
and (:lastName is null or c.last_nm = :lastName)
and (:state is null or ds.state = :state)
and (:status is null or lds.display_name = :status);

Access Run Time Error 3464 data type mismatch in criteria expression

What is wrong with that code? I can't figure out why I keep getting this error.
Sub renttt()
Dim rent_list As Recordset
Dim query As String
query = "SELECT * FROM (Rent INNER JOIN Movies ON Rent.Movie_ID = Movies.ID) INNER JOIN Customers ON Rent.Customer_ID = Customers.ID WHERE Rent.Movie_ID = '" & txtbxmovieID.Value & "' AND Rent.Date_Returned is Null;"
Set rent_list = CurrentDb.OpenRecordset(query)
If rent_list.RecordCount = 1 Then
rent_List.MoveFirst
txtbxname.Value = (rent_list![CusName])
txtbxsurname.Value = (rent_list![Surname])
txtbxcardID.Value = (rent_list![Id_Card_number])
txtbxaddress.Value = (rent_list![Address])
txtbxrented.Value = (rent_list![Date_Rent])
End If
End Sub
Wouldn't MovieId be a numeric? If so, no quotes:
WHERE Rent.Movie_ID = " & txtbxmovieID.Value & " AND ...

Access 2003 SQL Syntax error

I am new with access 2003 and been stuck on this query I have wrote for a while now. The tables and column names, operators and brackets i believe are all correct however I am getting a syntax error only after I inserted the following join operation
FROM (tDailyEntries
INNER JOIN tLEDGERS ON tLEDGERS.Action = tDailyEntries.ActionNo)
INNER JOIN (tProjects
below is my full code
SELECT DISTINCT tProjects.CC_IO AS ProjectNo,
Year([DateFrom]) & " Accrual " & MonthName(Month([DateFrom])) & " - "+[CompanyName] & " ( "+([LastName]) & ")" AS [Line/Item/Text],
tUsers.LastName AS Last_Name,
tDailyEntries.UserId AS UserID,
contractordailyrate AS DailyRate,
contractordailyhours AS Hours,
ROUND(contractordailyrate / contractordailyhours, 2) AS HourlyRate,
ROUND(SUM(tDailyEntries.CalculatedDailyHours), 2) AS MonthlyHours,
ROUND((HourlyRate * MonthlyHours), 2) AS Charge,
ROUND(Charge+ROUND((Charge*0.2),2),2) AS Accruals, tProjects.Project AS Project
FROM (tDailyEntries
INNER JOIN tLEDGERS ON tLEDGERS.Action = tDailyEntries.ActionNo)
INNER JOIN (tProjects
RIGHT JOIN (textcontractor
RIGHT JOIN (tTitle
RIGHT JOIN ((Location
RIGHT JOIN (tDepartments
RIGHT JOIN tUsers
ON tProjectType.ProjectTypeID = tProjects.ProjectTypeID)
ON tDepartments.DeptID = tUsers.DeptID)
ON tLocation.LocationID = tUsers.LocationID)
RIGHT JOIN (((tDailyEntries
LEFT JOIN tDepartments AS tDepartments_1
ON tDailyEntries.DeptCharged = tDepartments_1.DeptShortName)
LEFT JOIN tActions ON tDailyEntries.ActionNo = tActions.ActionID)
LEFT JOIN tLookups
ON tDailyEntries.Zone = tLookups.LookupID)
ON tUsers.UserID = tDailyEntries.UserID)
LEFT JOIN textmain
ON tUsers.UserID = textmain.userID)
ON tTitle.TitleID = tUsers.TitleID)
ON textcontractor.companyid = textmain.contractorcompany)
ON tProjects.ProjectID = tDailyEntries.ProjectNo
WHERE tTitle.TitleID = 37
AND Month([DateFrom]) = MonthNum
AND Day([DateFrom]) <21
GROUP BY tProjects.CC_IO, Year([DateFrom]) & " Accrual " & MonthName(Month([DateFrom])) & " - "+[CompanyName] & " ( "+([LastName]) & ")", tUsers.LastName, tDailyEntries.UserId, textmain.contractordailyrate,
Month([DateFrom]), textmain.contractordailyhours, tProjects.Project;
Any help would be great.