Solved

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

Posted on 2011-02-11
4
404 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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