Results 1 to 6 of 6

Thread: What is wrong with this code

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    6

    What is wrong with this code

    Hello,

    For last few days I'm trying everything to solve problem with this code but I still can't find any solution. Please, if you know what could it be, help.

    There is code:

    Code:
    Dim ArtikalId, StopaId As Integer
    Dim DefCijena, DefStopa As Double
    If Me.NarudzbeKupacaStavkeDgr.Columns(e.ColumnIndex).Name = "Artikal" Then
    
                ArtikalId = Me.NarudzbeKupacaStavkeDgr.Rows(e.RowIndex).Cells("Artikal").Value
                Dim ArtikliRedak As DataRow = Me.NarudzbeKupacaDataSet.Tables("ListaArtikli").Rows.Find(ArtikalId)
                If ArtikliRedak IsNot Nothing Then
                    DefCijena = ArtikliRedak("ArtikalCijena")
                    NarudzbeKupacaStavkeDgr.Rows(e.RowIndex).Cells("Cijena").Value = DefCijena
                    StopaId = ArtikliRedak("ArtikalStopa")                
                End If
    
                Dim StopeRedak As DataRow = Me.NarudzbeKupacaDataSet.Tables("SifarnikArtikli_StopePoreza").Rows.Find(StopaId)
                If StopeRedak IsNot Nothing Then
                    DefStopa = StopeRedak("StopaStopa") 
                    NarudzbeKupacaStavkeDgr.Rows(e.RowIndex).Cells("PdvStopa").Value = DefCijena
                End If
    
            End If
    I got value of ArtikalId as well as DefCijena and StopaId but I don't get value of DefStopa. I checked names and values, there should be value of DefStopa.

    When I put second part of this code, which should provide me value of DefStopa in try..catch like this:

    Code:
    Try
                    Dim StopeRedak As DataRow = Me.NarudzbeKupacaDataSet.Tables("SifarnikArtikli_StopePoreza").Rows.Find(StopaId)
                    DefStopa = StopeRedak("StopaStopa")
                    NarudzbeKupacaStavkeDgr.Rows(e.RowIndex).Cells("PdvStopa").Value = DefStopa 
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
    I got message: "Object reference not set to an instance of an object".

    Thanks in advance.
    Last edited by Mirnes; Jan 3rd, 2009 at 10:04 AM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: What is wrong with this code

    comment out the try & catch like so....
    Code:
    'Try
                    Dim StopeRedak As DataRow = Me.NarudzbeKupacaDataSet.Tables("SifarnikArtikli_StopePoreza").Rows.Find(StopaId)
                    DefStopa = StopeRedak("StopaStopa")
                    NarudzbeKupacaStavkeDgr.Rows(e.RowIndex).Cells("PdvStopa").Value = DefStopa 
                'Catch ex As Exception
                    'MsgBox(ex.Message)
                'End Try
    I'm going to guess that it errors on
    DefStopa = StopeRedak("StopaStopa")

    It could be that there's nothing in StopeRedak .... in your first code block, you were checking to see if it was nothing (which is good) ... since it was nothing, it passes over the code inside the if block ... which is why DefStopa doesn't get set. When you re-arranged it, you no longer were checking for it, so when you accessed StopeRedak, it errors out.

    Look to your .Find command to make sure your criteria is correct, and that you are getting something when you expect it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    6

    Re: What is wrong with this code

    Yes, a problem is with that line of code, that's sure. But, I use try...catch only to see what is going on. Actually, I'm going to use this code:

    Code:
    Dim StopeRedak As DataRow = Me.NarudzbeKupacaDataSet.Tables("SifarnikArtikli_StopePoreza").Rows.Find(StopaId)
                If StopeRedak IsNot Nothing Then
                    DefStopa = StopeRedak("StopaStopa")                 
                    NarudzbeKupacaStavkeDgr.Rows(e.RowIndex).Cells("PdvStopa").Value = DefCijena
                End If
    If I put Msgbox(StopaId) in front of this code, I got message that value of that variable is 7. So, I checked and there is record in SifarnikArtikli_StopePoreza with Id=7 and that table existing in NarudzbeKupacaDataSet. Also, there is column StopaStopa in that table and also there is column PdvStopa in Datagrid NarudzbeKupacaStavkeDgr. I checked names some 50 times and values and all of small possibilities that could go wrong but everything is OK. So, what else could be wrong? I really need some help above this, I stuck with it for few days and this part is crucial for my project, I can't go further with it until I solve this.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: What is wrong with this code

    Put a breakpoint on the line where DefStopa is being set. When the breakpoint is reached, highlight the StopeRedak("StopaStopa") portion and press Shift+F9 to take a look at what is being returned. Either you will never reach the breakpoint, which would only happen if StopeRedak is Nothing (which you say is not the case), or you will see what is being put into the variable, which should be informative.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    6

    Re: What is wrong with this code

    Thanks for your replies. It seems that there is nothing wrong with the code but with the dataset. I tried this code:

    Code:
                Dim StopeRedak As Integer
                StopeRedak = Me.ArtikliDataSet.Tables("SifarnikArtikli_StopePoreza").Rows.Count
                MsgBox(StopeRedak)
    and it is zero. How could it be if records exist in that table?

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    6

    Re: What is wrong with this code

    I found solution for this problem and it is very simple: This table has to have tableadapter included in the form

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