Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

hashtable as shared field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James - 20 Sep 2007 12:04 GMT
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.
Michael Nemtsev, MVP - 20 Sep 2007 15:05 GMT
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>
James - 20 Sep 2007 17:58 GMT
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.

"Michael Nemtsev" <Michael Nemtsev>, "MVP" wrote:

> Hello james,
>
[quoted text clipped - 19 lines]
> j> save method from the same web-page, etc.
> j>
Peter Duniho - 20 Sep 2007 19:33 GMT
> 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
James - 21 Sep 2007 06:44 GMT
> > Michael,
> > Normally just as a shared field.
[quoted text clipped - 10 lines]
>
> 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}
Peter Duniho - 21 Sep 2007 08:26 GMT
>> You should post a concise-but-complete example of code that reliably
>> demonstrates the problem.
[quoted text clipped - 7 lines]
> 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
Michael Nemtsev, MVP - 21 Sep 2007 12:09 GMT
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>
j> "Michael Nemtsev" <Michael Nemtsev>, "MVP" wrote:
j>
>> Hello james,
>>
[quoted text clipped - 22 lines]
>> j> save method from the same web-page, etc.
>> j>
James - 23 Sep 2007 07:50 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.