Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Complete Life Cycle for all the controls 2 45
Adjust the codes 3 63
Need help for captcha 2 42
advancedDataGridView / cancel / filter / using / button 5 31
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 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