Results 1 to 22 of 22

Thread: Now THIS is a weird problem! [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Now THIS is a weird problem! [Resolved]

    The problem is simple, the text is long.

    For this problem, I'll need you to actually download the attached ZIP file. It's VB.NET source code, so yeah, go ahead and look at it before you run it. It works with the Northwind database, so you shouldn't have any problems there.

    It consists of a form1.vb, and a class1.vb.

    First, the problem:

    Load up the form. It'll load everything, listbox, combo boxes, textboxes, everything.

    Now, you'll have the first record loaded up: Product "Chai" (btw, that's the Persian word for Tea)

    Without editing anything, press the update button, and watch the Supplier's combobox change value! The Category combobox does not, even though I'm using the same technique to udpate them.

    Also, you'll notice that the form's title has changed to the .SelectedIndex of the combobox.

    Press Update again.

    it's just going to keep changing. It goes in a very weird loop.

    If I use a rougher technique to do the update it works fine. (which you will find commented out underneath the actual line being used)



    Can someone take a look? It's absolutely baffling.
    Attached Files Attached Files
    Last edited by mendhak; Jun 24th, 2004 at 06:34 AM.

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Sorry, the commented line is not in the ZIP file.

    Here it is, the first line has to be replaced with the second line, and it'll start working properly.

    VB Code:
    1. 'dr("SupplierID") = CType(cboSupplier.SelectedItem, ListItemNumeric).ID
    2.         dr("SupplierID") = CInt(cboSupplier.SelectedIndex + 1)

  3. #3
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    You put the joint out and it came to you right?
    Magiaus

    If I helped give me some points.

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Nah, that joint's been permanently welded to my fingers.

    Any idea why I'm getting that odd problem?

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    nope. I haven't done anything like that yet. If I get time today I'll check it out though.
    Magiaus

    If I helped give me some points.

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by mendhak
    Sorry, the commented line is not in the ZIP file.

    Here it is, the first line has to be replaced with the second line, and it'll start working properly.

    VB Code:
    1. 'dr("SupplierID") = CType(cboSupplier.SelectedItem, ListItemNumeric).ID
    2.         dr("SupplierID") = CInt(cboSupplier.SelectedIndex + 1)
    Hmmm...bad code.
    That's basing it on the fact that the 1st index in the DB is 1 and that NON are missing. What if the index goes 1,2,3,7,14,23,24,25...your selectedindex won't work.

    What on earth is the purpose of your class1 (ListItemNumeric)???!!!

    Wpopooof

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by Wokawidget
    Hmmm...bad code.
    That's basing it on the fact that the 1st index in the DB is 1 and that NON are missing. What if the index goes 1,2,3,7,14,23,24,25...your selectedindex won't work.

    What on earth is the purpose of your class1 (ListItemNumeric)???!!!

    Wpopooof
    That is not the issue, what I've shown here is a very crude workaround. THeoretically, I should not be having to resort to such a technique, and be using the commented line instead. But it just doesn't work.

    ListItemNumeric is a class I wrote that can hold a Value and an ID, so that I can populate my listbox with a bunch of ListItemNumeric objects that have a displayed value, and an underlying ID, and that takes care of the situation you mentioned, where the IDs are not in an order.

    The line:

    VB Code:
    1. CType(cboSupplier.SelectedItem, ListItemNumeric).ID

    takes the currently selected item in the listbox, and gets the ID out.

    Good?

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Btw, did you run the sample? Are you seeing the same problem as I mentioned?

  9. #9

  10. #10

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by Wokawidget
    Yes and yes. I am still failing to see why you are using a class...???
    Woof
    I'll try to explain:

    As you're seeing, the listbox on the left has a lot of values in it. Now everytime you click on another item in the listbox, the values in the textboxes and comboboxes change. For this, the database needs to be requeried each time.

    And most logically, to query the database for the values corresponding to any particular item in the listbox, we need a Primary Key, correct?

    I needed a place to store the primary key. So, since the listbox actually accepts objects, I declared a class that would hold a .VALUE property (which is what you are seeing, "Chai") and a .ID property (which you don't see), used when querying.

    Try to think of another way to do this same thing, you'll find that this is an ideal way to do it.

    In my opinion at least. I'm new to VB.NET, so it's possible that there is a better way. So far, I don't see a better way.

    Comprende vous, smelly badger?
    *SLAP*

  11. #11

  12. #12
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    You muppet!
    You are saving the CORRECT ID's to the DB...but when you load them you are treating the ID as the LISTINDEX! Hahahaha
    VB Code:
    1. Private Sub FindItem(ByVal cboName As ComboBox, ByVal strID As String)
    2.  
    3.  
    4.         Try
    5.             cboName.SelectedIndex = CInt(strID) - 1
    6.         Catch ex As Exception
    7.             MessageBox.Show(ex.Message.ToString)
    8.         End Try
    9.  
    10.  
    11.  
    12.  
    13.     End Sub
    Wrong wrong wrong

    Woof

  13. #13

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by Wokawidget
    You muppet!
    You are saving the CORRECT ID's to the DB...but when you load them you are treating the ID as the LISTINDEX! Hahahaha
    VB Code:
    1. Private Sub FindItem(ByVal cboName As ComboBox, ByVal strID As String)
    2.  
    3.  
    4.         Try
    5.             cboName.SelectedIndex = CInt(strID) - 1
    6.         Catch ex As Exception
    7.             MessageBox.Show(ex.Message.ToString)
    8.         End Try
    9.  
    10.  
    11.  
    12.  
    13.     End Sub
    Wrong wrong wrong

    Woof
    Ok, then how should I be doing it?

  14. #14
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    1stly if your FormShow use:
    VB Code:
    1. Call FindItem(cboSupplier, CLng(dr("SupplierID").ToString))
    2. Call FindItem(cboCategory, CLng(dr("CategoryID").ToString))
    Then use:
    VB Code:
    1. Private Sub FindItem(ByVal cboName As ComboBox, ByVal lngID As Long)
    2.         Try
    3.             Dim lngIndex As Long
    4.             Dim lItem As ListItemNumeric
    5.             For lngIndex = 0 To cboName.Items.Count - 1
    6.                 lItem = CType(cboName.Items.Item(lngIndex), ListItemNumeric)
    7.                 If lItem.ID = lngID Then
    8.                     cboName.SelectedIndex = lngIndex
    9.                     Exit For
    10.                 End If
    11.             Next lngIndex
    12.         Catch ex As Exception
    13.             MessageBox.Show(ex.Message.ToString)
    14.         End Try
    15.     End Sub
    Works for me? Does it work on your pc?
    Woof

  15. #15

  16. #16

  17. #17

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I don't know how you got that to work. Set your Option Explicit to ON.

    corrected code:

    VB Code:
    1. Try
    2.             Dim lngIndex As Long
    3.             Dim lItem As ListItemNumeric
    4.             For lngIndex = 0 To cboName.Items.Count - 1
    5.                 ' lItem = CType(cboName.Items.Item(lngIndex), ListItemNumeric)
    6.                 lItem = CType(cboName.Items(lngIndex), ListItemNumeric)
    7.                 If lItem.ID = CInt(strID) Then
    8.                     cboName.SelectedIndex = lngIndex
    9.                     Exit For
    10.                 End If
    11.             Next lngIndex
    12.         Catch ex As Exception
    13.             MessageBox.Show(ex.Message.ToString)
    14.         End Try



    And looks like this was the way I was supposed to do it. Apparently, the moral of the story is: Do NOT do things the lazy way!

  18. #18

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You're an unwittingly smart badger.

    Oh, and I'm not "better at .NET" than you are. I'm a newbie too, still learning.

    Want a joint?

  19. #19
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    What do you mean?
    What's wrong with:
    VB Code:
    1. lItem = CType(cboName.Items.Item(lngIndex), ListItemNumeric)
    Item is a property of Items...I don't see what the problem is here.
    Item is just the default property of Items, thus Items(1) = Items.Item(1)...Woof
    If you have Items.Item(1) and you delete the . and then press . then the .NET IDE does NOT take you to the Item property in the drop down auto thingy...VB6 does...stupid .NET

    I am sure I have option explicit on...

    Am I missing something?

    Groiwl

  20. #20

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Hold on... I'll give it a try.

    {5 mins later}

    Yes, that is correct. Same difference. I got the problem here:

    VB Code:
    1. If lItem.ID = lngID Then

    lngID was never declared, so I don't know how it worked for you. I changed it to strID, and modified the line you mentioned out of personal preference.


  21. #21
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Look at the function I posted:
    VB Code:
    1. Private Sub FindItem(ByVal cboName As ComboBox, ByVal lngID As Long)
    lngID IS declared.
    You must have done something wrong...
    U had strID in your code.
    I changed this to lngID since it is a long and there is no point in passing it as a string only to convert it back to a long.

    I would love a joint

    Got given some leaf free the other day

    WOOOOoooooooooooooooof

  22. #22

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by Wokawidget

    Got given some leaf free the other day

    WOOOOoooooooooooooooof
    You lucky SOB! Here, leaves are so rare, they bleed.

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