looping through a data table vb - vb.net

I'm trying to loop through a data table that has multiple values for my constraint. How can I keep the first value and add together all the other values that match my constraint.
For i = 0 To ds.Tables(0).Rows.Count - 1
If ds.Tables(0).Rows(i).Item("TEND_POS_ID") = 8 Then
'This only returns the last value
'Value 1 = 2
'Value 2 = 7.5
'should = 9.5 but it is showing 7.5
tmpCoupon = ds.Tables(0).Rows(i).Item("TENDER_AMT")
End If
Next
txtCoupon.Text = tmpCoupon

If I understand your question correctly, you need to add the values of TENDER_AMT where the value in TEND_POS_ID is 8. If this is the case you could use the Select method of the DataTable
Dim rows = ds.Tables(0).Select("TEND_POS_ID = 8")
for each r in rows
tmpCoupon += Convert.ToDecimal(r("TENDER_AMD"))
Next
However, rembember that this kind of work (SUM a column grouping by another column) is usually better resolved by the query that you submit to the database. Something like this:
SELECT TEND_POS_ID, SUM(TENDER_AMD) FROM Table1 GROUP BY TEND_POS_ID

Related

How can I update if value is different or empty

I want to update my column if the vlaue is different from last value or its empty. I came up with this sql but it gives this error:
missing FROM-clause entry for table "box_per_pallet"
SQL:
UPDATE products AS p
SET box_per_pallet[0] = (CASE WHEN p.box_per_pallet.length = 0 THEN 0 ELSE p.box_per_pallet[0] END)
WHERE sku = 'A' AND store_id = 1
This is what I came up with based on your input. ARRAY_LENGTH takes the array and the dimension you want to check the length of as parameters. This missing from clause is because Postgres thinks that p.box_per_pallet is something other than an array and it can't find that anywhere in the query. You can't use the dot operator on arrays like p.box_per_pallet.length. It's like saying, "find the length field on table box_per_pallet in schema p".
UPDATE products
SET box_per_pallet[0] = CASE WHEN ARRAY_LENGTH(box_per_pallet, 1) = 0
OR box_per_pallet IS NULL
OR box_per_pallet[0] <> 0 -- your new value?
THEN 0
ELSE box_per_pallet[0]
END
WHERE sku = 'A'
AND store_id = 1
;
Here is a link to a dbfiddle showing the idea.

How to get Average of records from Access Database in vb.net?

I have a project where I connected an Access Database through a DataGridView. I've made some queries based on info inputed by the user through textboxes and comboboxes. Now I need to find a way to count the Average of the records found after the query from one specific column. Is there a way to do that ?
Store the counts from your queries...
Dim lstCounts As New List(Of Integer)
'Your database retrieval method: SELECT COUNT(*) FROM table WHERE field = 'Blah'
lstCounts.Add(<above result>)
'Your database retrieval method: SELECT COUNT(*) FROM table WHERE field = 'Blah1'
lstCounts.Add(<above result>)
'Your database retrieval method: SELECT COUNT(*) FROM table WHERE field = 'Blah2'
lstCounts.Add(<above result>)
'etc.
Find the average...
Dim nTotal As Integer = 0
Dim dAverage As Decimal = 0.0
For i As Integer = 0 to lstCounts.Count - 1
nTotal += lstCounts(i)
Next
'Make sure you aren't dividing by zero
If lstCounts.Count > 0
dAverage = nTotal / lstCounts.Count
End If
You could also simply just add the total as you perform each query and not bother using a List, but then you need to track how many queries you ran.

update column with numbers in sqlite table

This should be very simple but I cannot figure out how to do it. I would like to modify the values of two different columns. One from 1 to the total number of rows and the other one from the total of rows to one (basically increasing and decreasing number). I tried:
start = 0
end = number_of_rows + 1
c.execute('SELECT * FROM tablename')
newresult=c.fetchall()
for row in newresult:
start += 1
end -= 1
t = (start,)
u = (end,)
c.execute("UPDATE tablename SET Z_PK = ?", t) ---> this will transform all rows with Z_PK since there is no where statement to limit
c.execute("UPDATE tablename SET Z_OPT = ?", u)
The thing is that I don't know how I can add the "where" statement since I have no values I am sure for rows (like IDs number). A possibility would be to return the current row as the argument for "where" but I don't know how to do it...
Without an INTEGER PRIMARY KEY column, your table has an internal rowid column, which already contains the values you want:
UPDATE MyTable
SET Z_PK = rowid,
Z_OPT = (SELECT COUNT(*) FROM MyTable) + 1 - rowid

Concurrency violation - the UpdateCommand affected 0 of the expected 1 records

I am working on a VB.NET project and I am trying to Insert / Update some rows in the SQL DB.
To do this I use the code below to bring back a dataset based on 3 parameters.
If the dataset brings no data back, then the code simply inserts a new row.
This works fine.
However, if the data brings a row back (meaning the row already exists in the table) then I want to update one of the values instead.
This is where I'm getting the error in the title of this post.
Can anyone help me with where I'm going wrong please?
Thank you in advance.
Dim x_Update As Boolean
AdaptSql = New Data.AdapterX(SQL_ConnectionString)
DS = New DatsetX
AdaptSql.Fill(DS, Number, PeriodID, TypeID)
If DS.tbl_A.Count > 0 Then
x_Row = DS.tbl_A(0)
x_Row.BeginEdit()
x_Update = True
Else
x_Row = DS.tbl_A.NewRow
End If
x_Row.Number = Number
x_Row.DateID = PeriodID
x_Row.TypeID = TypeID
x_Row.Value = Value
x_Row.UpdatedDate = Date.Now
If x_Update = False Then DS.tbl_A.Addtbl_ARow(x_Row)
x_Row.EndEdit()
AdaptSql.Update(DS)
x_Row = Nothing
DS.Dispose()
AdaptSql = Nothing
check the x_Row.RowState before call AdaptSql.Update(DS), for a newly created row, the row state should be "Added", while an existing record should have "Modified" since there was some updates to it.

VB.NET How to do changes to rows selected using LINQ?

I have the following code which selects some rows using LINQ depending on some conditions.
Dim cname
Dim Category
Dim cprice
Try
For i = 0 To mainDatatable.Rows.Count - 1
cname = mainDatatable.Rows(i)("cname")
Category = mainDatatable.Rows(i)("ccategory")
cprice = mainDatatable.Rows(i)("cprice")
Dim nameparts() As String = cname.Split(New String() {" "}, StringSplitOptions.None)
Dim query = From products In mainDatatable.AsEnumerable()
Where products.Field(Of Integer)("ccategory") = Category And products.Field(Of Double)("cprice") = cprice And products.Field(Of String)("cname").Contains(nameparts(0)) And products.Field(Of String)("cname").Contains(nameparts(1))
Select products
If query.Count > 1 Then
'then i want to do some changes to those rows
End If
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
The code is working perfectly but I need to do changes on the selected rows. I'm not sure how to do it, for example i have a column called gprice and I want it to set it to some value for all the selected rows and save changes to mainDatatable ,would aprreciate your help.
To make it clear, suppose the row count in the query is 6 then I want to make all the values for some column in the query = 1 and then update the values of the selected rows in the mainDatatable
------update
If you don't get it forget the above code and here is what I am trying to do
-Ii have a datatable it only have 2 columns product name and product price, now there are products with same name but different colors
-I want to group those products by pharsing the string name and check for other products with identical words in the name, only one word is allowed to differ which is the color,
-Also the price have to be the same between the products in that group and this is what the above LINQ code does and it does it perfectly
-My problem is i have added a third column called groupnumber, I want to add 1 for each product in the 1st group, 2 for each product in group 2nd and so on.