hashtable as shared field

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Trouble in the house.

I have a BL-component with different shared get and save methods.
When I get my data I put everything in a shared field. When I call the save
method my shared field(hashtable) is saying "length=error cannot obtain
value".
When I call my get method again then the field has the wright values.

Does anyone have any ideas what is happening.
I'm calling the get and save method from the same web-page, etc.
 
Hello james,

How is your hashtable stored between postbacks?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


j> Trouble in the house.
j>
j> I have a BL-component with different shared get and save methods.
j> When I get my data I put everything in a shared field. When I call
j> the save
j> method my shared field(hashtable) is saying "length=error cannot
j> obtain
j> value".
j> When I call my get method again then the field has the wright values.
j> Does anyone have any ideas what is happening. I'm calling the get and
j> save method from the same web-page, etc.
j>
 
Michael,
Normally just as a shared field.
I even tried to put the data in a session variable.
before I call my save method I cast it to a hashtable and when I check the
value when I come in the method then the value is gone.

very strange.
 
James said:
Michael,
Normally just as a shared field.
I even tried to put the data in a session variable.
before I call my save method I cast it to a hashtable and when I check the
value when I come in the method then the value is gone.

You should post a concise-but-complete example of code that reliably
demonstrates the problem.

That said, it seems clear that since the two methods produce different
behavior, they obviously aren't accessing the same variable. Without
seeing the code, it's impossible to say what the confusion is.

Pete
 
Peter Duniho said:
You should post a concise-but-complete example of code that reliably
demonstrates the problem.

That said, it seems clear that since the two methods produce different
behavior, they obviously aren't accessing the same variable. Without
seeing the code, it's impossible to say what the confusion is.

Pete
Pete For a moment I thought it were different variable because i was using
shared methods and a shared field but i changed that and instanciated the
class to be sure that it is the same variable i'm using but that didn't solve
the problem.
Then I tried to put the data getting from my get-method in a session
variable. When i call my put(save) method i cast the session variable to the
wright type but at the point of entering my method i loose all data. The
casting etc. works fine.

Here's some of the code.
It's a web-application that calls data from a database over a business layer.

WebPage-code:

Private Sub commissionToPay(ByVal calculationDate As Date)
Try
Dim result As DataTable
Dim isSaved As Boolean
If Session("Commission") Is Nothing Then
Session("Commission") = BL.getCommissionToPay(calculationDate, isSaved)
End If
If Not Session("Commission") Is Nothing AndAlso
Session("Commission").containskey(calculationDate) Then
If Session("Commission").ContainsKey("CommissionsToPay") Then
result = CType(Session("Commission")("CommissionsToPay"),
DataSet).Tables(0)
result.DefaultView.Sort = "EmployeeName"
dtgAll.DataSource = result.DefaultView
dtgAll.DataBind()
pnlAll.Visible = True
btnAcceptToPay.Visible = Not isSaved
btnExport.Visible = False
lblHdAllEmployeeID.Visible = True
End If
Else
lblError.Text = rStrings.GetString("NoResults")
End If
Catch ex As Exception
lblError.Text = ex.Message
lblError.Visible = True
pnlMsg.Visible = True
End Try
End Sub

Private Sub btnAcceptToPay_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAcceptToPay.Click
'Save commission and orders for faster generation of reports.
Try
Dim ht As Hashtable
ht = CType(Session("Commission"), Hashtable)
If BL.saveAcceptCommissionToPay(ht) Then
btnAcceptToPay.Visible = False
End If
Catch ex As Exception
lblError.Text = ex.Message
lblError.Visible = True
pnlMsg.Visible = True
End Try
End Sub


BL-code(partial)


Public Shared Function getCommissionToPay(ByVal calculationdate As Date,
ByRef isSaved As Boolean) As Hashtable
Dim result As DataSet
Dim htCommissions As Hashtable

Try
'First check if commission not saved yet.
result = DAL.Commission.getCommissionToPayOnDate(calculationdate)
If Not result Is Nothing Then
isSaved = True
htCommissions.Add("CommissionsToPay", result)
Return htCommissions
Else
isSaved = False
result = New DataSet
'check if result in cache
If _htCommissions Is Nothing OrElse _CalculatedFor <> calculationdate Then
htCommissions = getCommission(calculationdate)
Else
htCommissions = _htCommissions
End If
Catch ex As Exception
Throw
End Try
End Function

Public Shared Function saveAcceptCommissionToPay(ByVal ht As Hashtable) As
Boolean '(ByVal commissionData As Hashtable) As Boolean
Dim succesfull As Boolean = False
Dim CommissionToPay As DataSet
Dim commission As dsCommissions
Dim pcID, pcOrderID As Int64
Dim target As dsCommissions.TargetsRow
Dim realised As dsCommissions.SalesCountersRow
Dim corr As dsCommissions.OrderCorrectionRow
'Dim = _htCommissions


'Table0 = totals of commission and corrections
'table1 = total of corrections for 1 month
'Table2 = periods to print report
Try
'save totals & corrections
If Not ht Is Nothing Then
If ht.ContainsKey("CommissionToPay") Then
CommissionToPay = ht("CommissionToPay")

when i get in this method then the parameter ht display the value
{length=error: cannot obtain value) where at the point of casting it sais
{length=2}
 
James said:
Pete For a moment I thought it were different variable because i was using
shared methods and a shared field but i changed that and instanciated the
class to be sure that it is the same variable i'm using but that didn't solve
the problem.

It _must_ be a different variable. If it were actually the same
variable, you'd get the same value from it.

Some points:

* The code you posted is not what I'd call "concise-but-complete".
It is at the same time too much, as there's a lot of stuff in the code
that obviously has nothing to do with the issue you're seeing, and too
little (at least as near as I can tell) since the declaration of the
variable in question appears to be missing.

* If the two places that you are accessing the variable are in
different layers of your implementation -- for example, one place is the
web client code while the other is in your business logic side -- then I
would suggest there's a strong likelihood that the way those two
components work together is not the way you seem to expect them to.

In particular, while I don't have any first-hand experience at all with
..NET web development, it wouldn't surprise me at all to find out that
even "Shared" variables are not actually shared between the server and
client instances of classes. The i/o overhead supporting that would be
far too great.

As I said before, it's difficult to comment with any sort of
specificity, since there isn't a good code example here to talk about.
But you may want to consider the above points.

Pete
 
Hello james,

the normally shared field it that field which lived over the postback, and
seems your is not
As was noted it's better to provide the small but fully *worked* code demonstrated
the problem, because it's hard understand whats going on
But seems that all roots lead to the situation that you u refer to the null
instance.
Your variable died after some step

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


j> Michael,
j> Normally just as a shared field.
j> I even tried to put the data in a session variable.
j> before I call my save method I cast it to a hashtable and when I
j> check the
j> value when I come in the method then the value is gone.
j>
j> very strange.
j>
"MVP" wrote: said:
Hello james,

How is your hashtable stored between postbacks?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:
http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high
and we miss it, but that it is too low and we reach it" (c)
Michelangelo

j> Trouble in the house.
j>
j> I have a BL-component with different shared get and save methods.
j> When I get my data I put everything in a shared field. When I call
j> the save
j> method my shared field(hashtable) is saying "length=error cannot
j> obtain
j> value".
j> When I call my get method again then the field has the wright
values.
j> Does anyone have any ideas what is happening. I'm calling the get
and
j> save method from the same web-page, etc.
j>
 
dear guys,

I've found a work around.
I created a new function with the same parametere. In that function the
hashtable is filled with the values. I copied the code from the previous
function except of the iteration of the for next loop. this is were i call
again the previous function but instead i pass a dataset to that function.
This way it works fine. Thank you all.

Peter,
I'll keep your suggestions in mind. this was my first post and i didn't
exactly know what to post. thanks again.
 
Back
Top