Hi
I'm writing vb using VS2012 and I'm accessing an SQL database to retrieve total stock items by Stock Code to create a movements report between the database and the 3rd party warehouse.
I first check to see if that stock item exists on the database and if it does, I then sum the records into TotalQuantity...that's where the problem arises! My test database only has 4 records on it and the record that it falls over with is definitely on the database and does have a stock value!
My code is as follows:
cmd.CommandText = "Select * from Stock_Items WHERE Stock_Code = '" & Stock_Code & "'"
reader = cmd.ExecuteReader
If reader.HasRows Then
cmd.CommandText = "SELECT SUM(Stock_Quantity) as TotalQuantity from Stock_Items WHERE Stock_Code = '" & Stock_Code & "'"
reader = cmd.ExecuteReader
[highlight #CC0000]SQL_Stock_Quantity = reader.Item("TotalQuantity")[/highlight]
Invalid_Record = False
Else
Invalid_Record = True
End If
End If
The line highlighted returns an error stating that there are no rows...how can that be when I am validating that rows exist in the first selection command?
Any help to resolve and ease my frustration would be well received!
Thanks
Steve
I'm writing vb using VS2012 and I'm accessing an SQL database to retrieve total stock items by Stock Code to create a movements report between the database and the 3rd party warehouse.
I first check to see if that stock item exists on the database and if it does, I then sum the records into TotalQuantity...that's where the problem arises! My test database only has 4 records on it and the record that it falls over with is definitely on the database and does have a stock value!
My code is as follows:
cmd.CommandText = "Select * from Stock_Items WHERE Stock_Code = '" & Stock_Code & "'"
reader = cmd.ExecuteReader
If reader.HasRows Then
cmd.CommandText = "SELECT SUM(Stock_Quantity) as TotalQuantity from Stock_Items WHERE Stock_Code = '" & Stock_Code & "'"
reader = cmd.ExecuteReader
[highlight #CC0000]SQL_Stock_Quantity = reader.Item("TotalQuantity")[/highlight]
Invalid_Record = False
Else
Invalid_Record = True
End If
End If
The line highlighted returns an error stating that there are no rows...how can that be when I am validating that rows exist in the first selection command?
Any help to resolve and ease my frustration would be well received!
Thanks
Steve