PDA

Click to See Complete Forum and Search --> : Help with this code Please??


Neumee
Mar 8th, 2003, 01:30 AM
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!!


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!!

Lunatic3
Mar 9th, 2003, 02:56 PM
one way could be this:

Dim drSeek(), drCurrent 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( The_Filed_Containing_Part_Number = '" & partno &"'")
' if partno is numerical then the above line will be
' drSeek=oldform9.PartsNumberData.Tables(0).Select( The_Filed_Containing_Part_Number = " & partno )

If drSeek.Lenght>0 Then
Messagebox.Show("Already Exists")
Else
Messagebox.Show("Not Exists")
drCurent=oldform9.mPartsNumber.NewRow()
drCurrent("PAR_NUMBER") =partno
oldform9.mPartsNumber.Rows.Add(drCurrent)
End if

Neumee
Mar 9th, 2003, 07:27 PM
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..

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

Lunatic3
Mar 10th, 2003, 02:54 PM
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.

drSeek = oldform9.PartsNumberData.Tables(0).Select("PAR_NUMBER = '" & partno & "'")
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?


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


This will look better if you use:

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

Neumee
Mar 10th, 2003, 03:08 PM
I figured out what I needed to do to fix my problem..

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

Lunatic3
Mar 10th, 2003, 04:10 PM
Dim i, sum, cnt As Integer, dt As DataTable
dt = Datagrid1.DataSource
cnt = dt.Rows.Count
For i = 0 To cnt - 1
sum += dt.Rows(i)(INDEX_OR_NAME_OF_THE_COLOUMN_HERE)
Next
Label1.Text=sum.ToString