Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-11
4
Medium Priority
?
410 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

715 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