2

I have this code in a vb.net page and it works just fine. When I tried to copy and paste the html into a c# page, it doesn't work, not sure why, any clue?

<asp:TemplateField HeaderText="Decal Expiration Date"                                                       SortExpression="ExpirationDate">
    <ItemTemplate>
        <%#DisplayExpirationDate(Eval("DecalID"))%>
    </ItemTemplate>
</asp:TemplateField>

the asp:Template field is in a gridview. I have the function DisplayExpirationDate in the code behind. On the aspx source page, it underlines the line <%#DisplayExpirationDate(Eval("DecalID"))%> and tells me that the best overloaded method match has some invalid arguments. It works on the VB page, but not on the c# page.

Any help, explanation appreciated.

4
  • You're going to atleast give us the signature of the DisplayExpirationDate() method. Your input is not of the right type. Probably will need to be casted as an int if I had to take a guess. Commented Mar 9, 2012 at 20:39
  • I fixed it by using: <%#DisplayExpirationDate(Eval("DecalID").ToString())%> Commented Mar 9, 2012 at 20:40
  • 1
    DisplayExpirationDate has an input variable named DecalID which is of type string? Commented Mar 9, 2012 at 20:42
  • Oh no, you have string param for that? Commented Mar 9, 2012 at 20:42

2 Answers 2

4

You have to cast your value to the appropriate data type, e.g.

<%#DisplayExpirationDate((string)Eval("DecalID"))%> 

The method must accept an object of type object otherwise. In VB.NET it works as long as you compile without compiler option strict.

Sign up to request clarification or add additional context in comments.

Comments

1

VB.NET has a very...errmm...forgiving implicit cast (and other, mostly unrelated, but just as vexing) setting named Option Strict. This defaults to Off, so it will try - at runtime - to convert the return of Eval (which is System.Object) to whatever type your function requires.

C# takes the other approach, and makes you specify the cast. So, you should change it to:

<%#DisplayExpirationDate((string)Eval("DecalID"))%>

Note that, if you require a string, you can also do:

<%#DisplayExpirationDate(Eval("DecalID").ToString())%>

but, that's not exactly the same thing - it will fail if the value is null (Nothing in VB.NET), but succeed if it's DBNull. The previous version will do the opposite. I generally consider the haphazard use of DBNull and ToString as sloppiness, so I strongly encourage the first pattern and appropriate DBNull checking if warranted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.