Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / May 2008

Tip: Looking for answers? Try searching our database.

*Scratching My Head* - I need help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony K - 14 May 2008 00:33 GMT
I know I have asked a lot of questions here, but this one might puzzle
others.

I have a query that gets all products whose OnHand < MinRequired and grouped
by the supplier.  The query fills perfectly.
From that, I autogenerate a Purchase Order and for each supplier, I add the
associated products.

When I execute the program without breakpoints, all the products are added
to the first created purchase order even though the products do not belong
to that supplier.  Then, the other PO's are created but no products are
filled for the remaining suppliers.

IF I create a breakpoint where the supplier has changed, then continue, the
appropriate PO's are created with the correct products for each supplier.

Help, I don't know what is going on.

Entire GeneratePO_Click Routine:

***BEGINNING
       Dim row As DataGridViewRow
       Dim cmmInvMan As OleDbCommand
       Dim strSQL As String
       Dim maximumOnHand As Integer, unitsOrdered As Integer
       Dim drdTest As OleDbDataReader
       Dim recordCounter As Integer = 999990  'Temporary PO #
       Dim cnnInvMan As OleDbConnection = New
OleDbConnection(My.Settings.Inventory_management_databaseConnectionString)
       cnnInvMan.Open()
       Dim supplierID As Integer, productID As Integer, price As Decimal
       Dim supplier As String = "", productDescription As String = "", poID
As String = ""
       supplier =
CStr(Me.POGenerateDataGridView.Rows(0).Cells("SupplierName").Value)

       strSQL = "SELECT SupplierID FROM Suppliers WHERE SupplierName = '" &
supplier & "'"
       cmmInvMan = New OleDbCommand(strSQL, cnnInvMan)
       drdTest = cmmInvMan.ExecuteReader
       Do While drdTest.Read
           supplierID = CInt(drdTest.Item(0))
       Loop

       Me.Purchase_OrdersTableAdapter.Insert(recordCounter.ToString, "Auto
Generated PO", supplierID, _
                   Nothing, Date.Today(), Today.AddDays(5), Nothing,
Nothing, Nothing, False, Nothing)
       Me.Purchase_OrdersTableAdapter.Update(Me.Inventory_management_databaseDataSet)
       For Each row In POGenerateDataGridView.Rows

           If supplier <> row.Cells("SupplierName").Value.ToString Then
'***If Supplier is different
               supplier = CStr(row.Cells("SupplierName").Value)  '***If I
perform breakpoint here...IT WORKS.
               strSQL = "SELECT SupplierID FROM Suppliers WHERE
SupplierName = '" & supplier & "'"
               cmmInvMan = New OleDbCommand(strSQL, cnnInvMan)
               drdTest = cmmInvMan.ExecuteReader
               Do While drdTest.Read
                   supplierID = CInt(drdTest.Item(0))
               Loop
               recordCounter += 1  'Increment Temporary PO# for new PO.

               Me.Purchase_OrdersTableAdapter.Insert(recordCounter.ToString,
"Auto Generated PO", supplierID, _
                           Nothing, Date.Today(), Today.AddDays(5),
Nothing, Nothing, Nothing, False, Nothing)

           End If

           strSQL = "SELECT * FROM Products WHERE ProductIDNumber = '" &
row.Cells("ProductIDNumber").Value.ToString & "'"
           cmmInvMan = New OleDbCommand(strSQL, cnnInvMan)
           drdTest = cmmInvMan.ExecuteReader
           Do While drdTest.Read
               productID = CInt(drdTest.Item("ProductID"))
               productDescription =
CStr(drdTest.Item("ProductDescription"))
               maximumOnHand = CInt(drdTest.Item("MinimumRequired"))
               price = CDec(drdTest.Item("UnitPrice"))
           Loop

           strSQL = "SELECT SUM(UnitsReceived) - SUM(UnitsSold) -
SUM(UnitsShrinkage) " & _
                   "FROM [Inventory Transactions]" & _
                   "WHERE (ProductID = " & productID & ")"

           cmmInvMan = New OleDbCommand(strSQL, cnnInvMan)
           drdTest = cmmInvMan.ExecuteReader
           Do While drdTest.Read
               unitsOrdered = maximumOnHand - CInt(drdTest.Item(0))
           Loop
           cmmInvMan.Dispose()
           drdTest.Close()
           strSQL = "SELECT * FROM [Purchase Orders] WHERE
PurchaseOrderNumber = '" & recordCounter.ToString & "'"
           cmmInvMan = New OleDbCommand(strSQL, cnnInvMan)
           drdTest = cmmInvMan.ExecuteReader
           Do While drdTest.Read
               poID = CStr(drdTest.Item("PurchaseOrderID"))
           Loop

           Me.Inventory_TransactionsTableAdapter.Insert(Date.Today(),
productID, CType(poID, Integer), productDescription, price, _
                                   unitsOrdered, Nothing, Nothing, Nothing,
Nothing)

       Next

*****END

Thanks,
Tony K.
David Glienna - 14 May 2008 02:26 GMT
Probably need to use different recordsets.  I had a problem with a vb6 app
that was solved my not mixing sql in the same recordset

cmmInv1
cmmInv2
cmmInv3

Signature

David Glienna
MVP - Visual Developer (Visual Basic)
2006 thru 2008

>I know I have asked a lot of questions here, but this one might puzzle
>others.
[quoted text clipped - 112 lines]
> Thanks,
> Tony K.
Tony K - 14 May 2008 05:13 GMT
Hmmm... Alright.  I'll make the changes tomorrow at work.

Thanks David.

Tony K.

> Probably need to use different recordsets.  I had a problem with a vb6 app
> that was solved my not mixing sql in the same recordset
[quoted text clipped - 119 lines]
>> Thanks,
>> Tony K.
Jack Jackson - 14 May 2008 06:46 GMT
I don't know why setting a breakpoint causes different results.
However, if in the failure case all products are added to the first
purchase order, that suggests that maybe the SELECT to fetch the
purchase order just before the insert of the product fails to fetch
any records.  You should check for that case and do something, like
throw an exception.

There are a number of odd things about your code.

You have two sets of identical code to fetch supplierID given a
supplier, one done initially for the value in the DataGridView and the
other in the loop when supplier changes.  It would be better to
initially set supplier to a value to a value that can't occur, like
"", and just let the code in the loop fetch supplierID for the first
record.  You would also need to get rid of the insert of the purchase
order before the loop.

You have several sequences like:
>        Do While drdTest.Read
>            supplierID = CInt(drdTest.Item(0))
>        Loop
This sets supplierID to the value from the last record fetched.  I
presume that you only expect there to be one record returned, so why
bother with the loop?  Also in each case you should check to see if
you don't get any records back.

You never close the Command objects.

You build string WHERE clause values into the SQL string.  Unless you
can guarantee that the values of those strings don't come from user
input you are opening yourself to SQL injection attacks.  It is better
to use parameters.

>I know I have asked a lot of questions here, but this one might puzzle
>others.
[quoted text clipped - 110 lines]
>Thanks,
>Tony K.
Tony K - 14 May 2008 20:25 GMT
Jack,
   Thank you for your advice.  I will try the recommendations you have
provided.  Lastnight before this message came in, I coded a MessageBox each
time a new PO is created letting the user know that a new PO was created for
"XYZ Company".  Because there will usually be more than one PO created and
each PO has one Supplier, when a new supplier is detected within the For
Loop, I display a new MessageBox showing that a new PO was created for that
supplier.

   Again, I don't get why this works but because there is a pause (when the
user reads the message box) each PO is created successfully.

Thank you for your help,
Tony K.

>I don't know why setting a breakpoint causes different results.
> However, if in the failure case all products are added to the first
[quoted text clipped - 151 lines]
>>Thanks,
>>Tony K.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.