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.
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
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.
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.
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?
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 :sick: