Results 1 to 6 of 6

Thread: Help with this code Please??

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154

    Help with this code Please??

    Can someone please help me with this?? I'm trying to check if a part already exists in the dataset. This will work if add a part say "6969"....then the next part I add is "6969"..that works fine...but if there is a part "6969"...then another "2002"....then I try and add "2002" again....it still adds it..and it shouldn't...so if anywhere in the dataset the part number is contained...dont add new record!!


    Code:
    Dim drCurrent as New Datarow
    drCurrent = oldform9.mPartsNumber.NewRow()
    
    drCurrent("PAR_NUMBER") = DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0)
    
    If oldform9.mPartsNumber.Rows.Count < 1 Then
                    oldform9.mPartsNumber.Rows.Add(drCurrent)
                Else
                    Dim thisItem As System.Data.DataRow
                    For Each thisItem In oldform9.PartsNumberData.Tables(0).Rows
                                           If drCurrent("PAR_NUMBER") = oldform9.PartsNumberData.Tables(0).Rows.Item(0).Item(0).ToString() Then
                                MsgBox("equal")
                            Exit For
                        Else
                            MsgBox("not equal:")
                        oldform9.mPartsNumber.Rows.Add(drCurrent)
                            Exit For
                       End If
    Next
    End If

    Thanks for the help in advance!!

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    one way could be this:
    VB Code:
    1. Dim drSeek(), drCurrent as DataRow,
    2. Dim partno as String' I assumed part number is String
    3. partno=DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0)
    4. drSeek=oldform9.PartsNumberData.Tables(0).Select( The_Filed_Containing_Part_Number = '" & partno &"'")
    5. ' if partno is numerical then the above line will be
    6. ' drSeek=oldform9.PartsNumberData.Tables(0).Select( The_Filed_Containing_Part_Number = " & partno )
    7.  
    8. If drSeek.Lenght>0 Then
    9.     Messagebox.Show("Already Exists")
    10. Else
    11.    Messagebox.Show("Not Exists")
    12.    drCurent=oldform9.mPartsNumber.NewRow()
    13.    drCurrent("PAR_NUMBER") =partno
    14.    oldform9.mPartsNumber.Rows.Add(drCurrent)
    15. End if

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154
    Thanks for the reply!!

    The code you submitted works perfect....but now..I want to increase the QTY by 1, to the exsisting part if the part already exists?

    The following code is what I'm using..

    Code:
    Dim drSeek(), x As DataRow
                    Dim drSeekStock(), y As DataRow
                    Dim partno As String ' I assumed part number is String
    
                    partno = DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0)
                    drSeek = oldform9.PartsNumberData.Tables(0).Select("PAR_NUMBER = '" & partno & "'")
                    drSeekStock = oldform9.PartsNumberData.Tables(0).Select("STOCK = '" & partno & "'")
    
                    If drSeek.Length > 0 Then
                        MessageBox.Show("Already Exists")
                        oldform9.DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 3) =                     oldform9.DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 3) + 1 
                    Else
                        MessageBox.Show("Not Exists")
                        oldform9.mPartsNumber.Rows.Add(drCurrent)
                    End If
    This only works for the first record you add to the invoice...Is there someway I can Increase qty by 1, for the part that already exists that I'm trying to add.

    So If I add "6969"...then add it again...this will increase by 1..
    but then if I add "2002"....then try adding it again...it will throw an excpetion instead of increasing QTY by 1.

    Thanks for the help

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I could help more if I knew your data structure, and the description of the exception. However I can guess where your problem lies.
    When u add a value that does not exist at all, then you have to update your main table (Table(0) of dataset) with this value too, in that case furthure additions will be considered as 'already exists'. If you dont do that you may face an error trying to enter duplicate values in mPartsNumber table if you have chosen the filed to be unique. So check if the exception is thrown when you are adding a brand new part number then the likely problem will be that.

    There is a strange point in your code.
    VB Code:
    1. drSeek = oldform9.PartsNumberData.Tables(0).Select("PAR_NUMBER = '" & partno & "'")
    2. drSeekStock = oldform9.PartsNumberData.Tables(0).Select("STOCK = '" & partno & "'")
    in the above lines you are filtering two fileds of one table, "PAR_NUMBER" and "STOCK" based on the same value 'partno'. Do they contain the same data? if yes why two fileds with the same data, if not why filtering on same value?

    VB Code:
    1. oldform9.DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 3) = oldform9.DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 3) + 1

    This will look better if you use:
    VB Code:
    1. oldform9.DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 3) +=1

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154
    I figured out what I needed to do to fix my problem..

    Code:
    If oldform9.mPartsNumber.Rows.Count < 1 Then
                    oldform9.mPartsNumber.Rows.Add(drCurrent)
                Else
                    Dim drSeek()
                    Dim partno As String = ""
                    Dim invoiceRow As DataRow
                    partno = DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0)
                    drSeek = oldform9.PartsNumberData.Tables(0).Select("PAR_NUMBER = '" & partno & "'")
    
                    If drSeek.Length > 0 Then
                        invoiceRow = CType(drSeek(0), DataRow)
                        If invoiceRow("PAR_BO") <> 0 Then
                            invoiceRow.BeginEdit()
                            invoiceRow("PAR_BO") += 1
                            invoiceRow.EndEdit()
                        Else
                            invoiceRow.BeginEdit()
                            invoiceRow("STOCK") += 1
                            invoiceRow("PAR_AMOUNT") = invoiceRow("STOCK") * invoiceRow("PAR_LIST")
                            invoiceRow.EndEdit()
                         End If
                    Else
                        oldform9.mPartsNumber.Rows.Add(drCurrent)
                    End If
                End If
                oldform10.Close()
    The strange part in my code....was just for testing purposes...I dont actually use that.....

    And yeah I just learned the better looking syntax for "+="...so thanks alot.

    I have another problem though....if you can help me with that...

    I need to sum all the values of a column in the datagrid...
    So for the column "PAR_NUMBER"...I want to add all items in the datagrid and display the total in a label.

    ex...
    Datagrid....

    Item1 "200"
    Item2 "100"

    Label.text = "400" (sum of the 2 items)

    thanks again

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    VB Code:
    1. Dim i, sum, cnt As Integer, dt As DataTable
    2. dt = Datagrid1.DataSource
    3. cnt = dt.Rows.Count
    4. For i = 0 To cnt - 1
    5.     sum += dt.Rows(i)(INDEX_OR_NAME_OF_THE_COLOUMN_HERE)
    6. Next
    7. Label1.Text=sum.ToString

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width