Solved

How do I pass an array of objects to my (code behind) accessor method in ASP.Net?

Posted on 2011-02-11
4
403 Views
Last Modified: 2012-05-11
Hey Guys:

Hope y'all doing well.

I've got an ASP .Net/C# question

I've got the following accessor method on my base page code behind:
-------------------------------------------------------------------
public static string RetrieveTimeStampFromAudit (AuditRecord[] auditHistory, string operationTypeStringToFind, string format)
{
//TODO Add implementation
}

I'd like to be able to call it from my ASP.Net page, more or less
in the following fashion:
-----------------------------------------------------------------
Text='<%# RetrieveTimeStampFromAudit("Foobar.AuditRecord", "Created", "MM/dd/yyyy") %>'

 
I'm getting casting errors at page load time.
I’ve tried changing the Method definition to
                Object
                Object[]

and playing with the the actual call on the Asp .Net page.

Do you have any suggestions how to manipulate this cast/definition to work?

Thanks,
JB
0
Comment
Question by:jxbma
  • 3
4 Comments
 
LVL 12

Expert Comment

by:starlite551
ID: 34876769
Hi JB, Can you post the complete code for this method : RetrieveTimeStampFromAudit()
I have a Solution for You Plz try this code :

 
Text='<% (string)RetrieveTimeStampFromAudit("Foobar.AuditRecord", "Created", "MM/dd/yyyy") %>'

//OR

Text='<% RetrieveTimeStampFromAudit("Foobar.AuditRecord", "Created", "MM/dd/yyyy").ToString() %>'

//OR

Text='<%# (string)RetrieveTimeStampFromAudit("Foobar.AuditRecord", "Created", "MM/dd/yyyy") %>'


//OR

Text='<%# RetrieveTimeStampFromAudit("Foobar.AuditRecord", "Created", "MM/dd/yyyy").ToString() %>'

//OR

Text='<%# RetrieveTimeStampFromAudit("Foobar.AuditRecord", "Created", "MM/dd/yyyy") as String %>'

Open in new window



One of the above declarations may definitely work for you.. See since the return type of this method is a string I think it would definitely be necessary to cast it to a string object.
0
 
LVL 12

Accepted Solution

by:
starlite551 earned 500 total points
ID: 34876786
Hey I am sorry I did not notice a simple mistake that you have committed in the code block.. You have specified the first parameter as a string in the method whereas it should have been an Array object of AuditRecord class.. So the code would change to this :
Text='<% (string)RetrieveTimeStampFromAudit(Foobar.AuditRec, "Created", "MM/dd/yyyy") %>'

//OR

Text='<% RetrieveTimeStampFromAudit(Foobar.AuditRec, "Created", "MM/dd/yyyy").ToString() %>'

//OR

Text='<%# (string)RetrieveTimeStampFromAudit(Foobar.AuditRec, "Created", "MM/dd/yyyy") %>'


//OR

Text='<%# RetrieveTimeStampFromAudit(Foobar.AuditRec, "Created", "MM/dd/yyyy").ToString() %>'

//OR

Text='<%# RetrieveTimeStampFromAudit(Foobar.AuditRec, "Created", "MM/dd/yyyy") as String %>'

Open in new window

Here I assume Foobar.AuditRec is of the type AuditRecord[] like so :
public class Foobar
{
      public Foobar()
      {
          //Default Constructor..
      } 
      public Foobar(int size)
      {
           auditRec = new AuditRecord[size];
           //Manipulation code below here.. via AuditRec property
      }  
      private AuditRecord[] auditRec;

      public AuditRecord[] AuditRec
      {
            get 
            {
                 return auditRec;
            } 
            set
            {
                 auditRec = value; 
            }  
      }

}

Open in new window

0
 
LVL 12

Expert Comment

by:starlite551
ID: 34876799
Final Comment : Change the first parameter of the method :

public static string RetrieveTimeStampFromAudit (AuditRecord[] auditHistory, string operationTypeStringToFind, string format)
{
//TODO Add implementation
}

Open in new window


To an object of AuditRecord[] class like this one for example :

 
AuditRecord[] obj = new AuditRecord[10];

string test = RetrieveTimeStampFromAudit(obj,"Created","MM/dd/yyyy");

Open in new window


I think thats your issue..Its not able to cast the string "Foobar.AuditRecord" into an object of type AuditRecord[]
0
 
LVL 5

Expert Comment

by:aternex
ID: 34877037
Also, keep in mind you are using
Text='<%# RetrieveTimeStampFromAudit("Foobar.AuditRecord", "Created", "MM/dd/yyyy") %>'

Open in new window

to call your method. Depending on where that code appears in  your asp.net page, this may not work. Using the # will only work inside of a databound control, otherwise you would use
Text='<%= RetrieveTimeStampFromAudit("Foobar.AuditRecord", "Created", "MM/dd/yyyy") %>'

Open in new window

. I'm just throwing this out there, but it doesn't sound like it would produce the error you are receiving.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP.NET e-commerce website 4 29
Converting C# Objects into JSON file 7 39
VB.NET HttpWebRequest 12 32
Calling stored proc in EDMX 11 19
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now