Solved

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

Posted on 2011-02-11
4
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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